In this article we learn the string methods in java with examples. Java string class has a many methods but we are learn the most used string methods in java.
String Methods in Java with Examples
1) Length Method
This string method is particularly useful for finding out the length of the string. It returns an integer which is the length of the string.
Syntax:
<string variable>. length ();
Example:
String str1= “There is a garden and It is beautiful.”;
int length = str1.length();
System.out.println(“Str1 = “+length);
Output:
Str1 = 38
2) charAt Method
This string methods in java returns the character at the particular index passed as an argument to this method.
Syntax:
<string variable>. charAt(<index>);
Example:
String str = “There is a garden and It is beautiful.”;
System.out.println(“char1=”+str.charAt(1));
System.out.println(“char6=”+str.charAt(6));
Output:
char1=h
char6=i
Note: –
1) Indexing Start from 0.
2) In String, It Calculate space also.
3) Substring Method (int beginIndex)
The method returns the substring from the index which is passed as an argument to the method.
Syntax:
<String variable>. substring(<index>);
Example:
String str=”There is a garden and It is beautiful.”;
System.out.println(“Substring-5= “+str.substring(5));
Output:
Substring-5= is a garden and It is beautiful.
4) Substring (int begin Index, int end Index)
This string methods in java returns the substring which starts at the index given as the first argument to the method and ends at the index given as the second argument in the method.
Syntax:
<String variable>. substring(<index1>,<index2>);
Example:
String str=”There is a garden and It is beautiful.”;
System.out.println(Substring- [6,25] = “+str.substring(6,25));
Output:
Substring- [6,25] = is a garden and It.
5) Concat Method
As the name suggests, this string methods in java is useful for concatenating two strings. Concatenating means adding together two entities.
Syntax:
<String-variable1>.concat(<String_variable2>);
Example:
String strA= “Pune”;
String strB=”Mumbai”;
String strAB = strA.concat(” “+strB);
System.out.println(“strAB=”+strAB);
Output:
strAB=Pune Mumbai
6) indexOf Method
This string methods in java returns the index of the first occurrence of the character passed as an argument in the string.
Syntax:
<string_variable1>.indexOf(<String_variable2>);
Example:
String str=”There is a garden and It is beautiful.”;
System.out.println(” Index of ‘T’ = “+str.indexOf(‘T’));
System.out.println(” Index of ‘g’=”+str.indexOf(‘g’));
Output:
Index of ‘T’ = 0
Index of ‘g’=11
7) equals Method
This method returns true if both the strings are equal and false if they are not equal. Equals is a method that compares the actual content of the object.
Syntax:
<String_variable1>.equals(<String_variable2>);
Example:
String str1= new String(“Pune”);
String str2=”Pune”;
String str3= new String(“Pune”);
String str4=”Mumbai”;
System.out.println(“str1.equals(str2) = “+ str1.equals(str2));
System.out.println(“str1.equals(str3) = “+ str1.equals(str3));
System.out.println(“str1.equals(str4) = “+ str1.equals(str4));
Output:
str1.equals(str2) = true
str1.equals(str3) = true
str1.equals(str4) = false
8) == (equals) Method
This string methods in java returns true if both the strings are equal and false if they are not equal. == is an operator that compares the memory or reference location of an object in the heap.
Syntax:
<String_variable1>==(<String_variable2>);
Example:
String str1= new String(“Pune”);
String str2=”Pune”;
String str3= new String(“Pune”);
String str4=”Mumbai”;
String str5= str2;
System.out.println(“str1==(str2) = “+ (str1==(str2)));
System.out.println(“Str1==(Str3) = “+ (str1==(str3)));
System.out.println(“Str2==(Str5) = “+ (str2==(str5)));
Output:
str1.equals(str2) = false
str1.equals(str3) = false
str2.equals(str5) = true
9) compareTo Method
This method compares the two strings in a lexicographical order. If both of the strings are equal it returns zero. The result is positive if the first argument string is greater than the second string, lexicographically. If not, the result is negative.
Syntax:
<variable1>.compareTo(<variable2>);
Example:
String str1= new String(“Pune”);
String str2=”Pune”;
String str3= new String(“Mumbai”);
System.out.println(“str1.Comapre to (str2) = “+ str1.compareTo(str2)); System.out.println(“str1.equals(str3) = “+ str1.compareTo(str3));
Output:
str1.Comapre to (str2) = 0
str1.equals(str3) = 3
10) toLowerCase Method
This string methods in java Converts the string to lowercase.
Syntax:
<variable>. toLowerCase ();
Example:
String str=”There is a garden and It is beautiful.”;
System.out.println(” Lowercase=”+str1.toLowerCase());
Output:
Lowercase=there is a garden and it is beautiful.
11) toUpperCase Method
This string methods in java Converts the string to uppercase.
Syntax:
<variable>. toUpperCase ();
Example:
String str=”There is a garden and It is beautiful.”;
System.out.println(” Uppercase=”+str1.toUpperCase());
Output:
Uppercase=THERE IS A GARDEN AND IT IS BEAUTIFUL.
12) trim Method
Trims the string, i.e, removes all unnecessary spaces before and after the string. Note that it does not remove the spaces inside the string.
Syntax:
<variable>. trim ();
Example:
String str=“ There is a garden and It is beautiful. “;
System.out.println(” Original String=”+str1);
System.out.println(” Trimmed String=”+str1.trim());
Output:
Original String= There is a garden and It is beautiful.
Trimmed String=There is a garden and It is beautiful.
13) replace Method
This string methods in java replaces each occurrence of the first argument of the string with the second argument and returns the resulting string.
Syntax:
<String_variable>. replace(<character1>,<character2>);
Example:
String str=“There is a garden and It is beautiful.”;
System.out.println(“1) Replace ‘i to z’=”+str.replace (“i”, “z”));
System.out.println(“2) Replace ‘is to was’=”+str.replace(“is”, “was”));
System.out.println(” 3) ReplaceFirst ‘is to was’=”+str.replaceFirst (“is”, “was”));
System.out.println(“4) ReplaceAll ‘is to was’=”+str.replaceAll (“is”, “was”));
Output:
1) Replace’i to z’=There zs a garden and It zs beautzful.
2) Replace’is to was’=There was a garden and It was beautiful.
3) ReplaceFirst’is to was’=There was a garden and It is beautiful.
4) ReplaceAll’is to was’=There was a garden and It was beautiful
14) Contains Method
The “contains ()” method searches the sequence of characters in this string.
It returns true if the sequence of char values is found in this string otherwise returns false.
“Boolean” is the data type of “contains ()” method.
Syntax:
<String_variable>. Contains (CharSequence);
Example:
String str= ” There is a garden and It is beautiful.”;
System.out.println(str. contains (“and”));
System.out.println(str. contains (“hello”));
Output:
true
false
15) EndsWith Method
This string methods in java checks whether the String ends with a specified suffix. This method returns a boolean value true or false. If the specified suffix is found at the end of the string, then it returns true else it returns false.
Syntax:
<String_variable>. endsWith (CharSequence);
Example:
String str= ” There is a garden and It is beautiful.”;
System.out.println(str. endsWith (“beautiful “));
System.out.println(str. endsWith (“and”));
Output:
true
false
16) spilt Method
The java string split() method splits this string against given regular expression and returns a char array.
regex − the delimiting regular expression.
Syntax:
<String_variable>. spilt (String regex);
Example:
String str= ” There is a garden, and It is beautiful.”;
String [] ar=str. split(“r”);
int size= ar.length;
System.out.println(“Number of Words =”+size);
for (int i=0; i<ar. length; i++) {
System.out.println(ar[i]);
}
Output:
The
e is a ga
den and it is beautiful.