Java – String

In this Java String tutorial, we will learn how to create a String, how to accept String input in Java, useful String methods and many more about String with examples.

What is a String?

In the computer programming world, text values are known as a String. So we can say String is a sequence of characters. A string is a class in Java and text values are stored as an object of String class.

How to Create a String Object?

In Java, there are many different ways to create a String.

//create and assign as string in a variable.
String msg="Learn to Code";

//by creating String object.
String msg=new String("Learn to Code");

//using characters array
char ch[] = {'J','a','v','a'};
String str=new String(ch);

Accept String Input in Java.

import java.util.*;
class Example
{
public static void main(String arg[])
{
	String msg;
	//create Scanner object
	Scanner obj=new Scanner(System.in);
		
	System.out.println("Enter Your Message");
	//read String input
	msg=obj.nextLine();
	System.out.println(msg);
}
}		 

String Methods in Java.

  • length() – The length() method of String class returns the length of the given string.
  • equals() – The equals() method compare two Strings and returns true if and only if both String objects are the same (Small and Capital letters are not the same).
  • equalsIgnoreCase() – String equalsIgnoreCase() method compares two and returns true if both the strings are same, it ignores case (Small and Capital letters are the same).
  • charAt(int index) – The charAt(index) method of String returns the character at the given index.
  • substring() – In Java, substring() method of String class is used to create a substring from the given string.
  • concat() – String concat() method concatenates two strings and returns it.
  • toLowerCase() and toUpperCase() – These methods are used to changed the case of strings.
  • indexOf() – The index() method returns the index of given character. There are four different ways to use indexOf() method.
    1. indexOf(int ch): returns the first index of the character in the string, otherwise returns -1 if the character is not present.
    2. indexOf(int ch, int from): returns the index of the character after the given second parameter, from the index.
    3. indexOf(String str): returns the index of the substring in the String.
    4. indexOf(String str, int from): returns the index of the substring after the given from index.

Example: String Method example in Java.

class Example
{
public static void main(String arg[])
{
	String msg="Hello World of Java";
	int len=msg.length();
	System.out.println(len);
	//print length of string

	String str="Coder Mantra";
	char ch = str.charAt(2);
	System.out.println(ch);
	//return character d at index 2

	String w = "Java Example";
	String e1 = w.substring(5);
	System.out.println(e1);
	//Example
 
	String e2 = w.substring(5, 8);
	System.out.println(e2);
	//Exa

	String s = "Java".concat("Example");
	System.out.println(s);
	//It will print JavaExample

}
}