String class functions
The following methods are some of the most commonly used methods of String class.
charAt()
charAt() function returns the character located at the specified index.
String str = "studytonight";
System.out.println(str.charAt(2));
Output : u
equals()
equals() determines the equality of two Strings, the function is case-sensitive.
String str = "java";
System.out.println(str.equals("java"));
Output : true
String str = "JAVA";
System.out.println(str.equals("java"));
Output : false
equalsIgnoreCase()
equalsIgnoreCase() determines the equality of two Strings, ignoring thier case (upper or lower case doesn’t matters with this fuction ).
String str = "java";
System.out.println(str.equalsIgnoreCase("JAVA"));
Output : true
length()
length() function returns the number of characters in a String.
String str = "Count me";
System.out.println(str.length());
Output : 8
replace()
replace() method replaces occurances of character with a specified new character.
String str = "Change me";
System.out.println(str.replace('m','M'));
Output : Change Me
substring()
substring() method returns a part of the string. substring() method has two forms,
public String substring(int begin);
public String substring(int begin, int end);
The first argument represents the starting point of the subtring. If the substring() method is called with only one argument, the subtring returned, will contain characters from specified starting point to the end of original string.
But, if the call to substring() method has two arguments, the second argument specify the end point of substring.
String str = "0123456789";
System.out.println(str.substring(4));
Output : 456789
System.out.println(str.substring(4,7));
Output : 456
toLowerCase()
toLowerCase() method returns string with all uppercase characters converted to lowercase.
String str = "ABCDEF";
System.out.println(str.toLowerCase());
Output : abcdef
toUpperCase()
toUpperCase() method returns string with all uppercase characters converted to lowercase.
String str = "abcdef";
System.out.println(str.toLowerCase());
Output : ABCDEF
concat()
concat() method is used to join two strings.
String s1, s2, s3;
s1 = "Computer";
s2 = "Application";
s3 = s1.concat(s2);
System.out.println("Output: "+s3);
Output : ComputerApplication
trim()
This method returns a string from which any leading and trailing whitespaces has been removed.
String str = " hello ";
System.out.println(str.trim());
Output : hello
compareTo()
compareTo() methods compares two strings and returns the first difference between corresponding characters. If all the characters are same then it returns 0. The return type of this function is int.
Also when one string is completely present in the second string then the function returns the difference of the lengths of the strings.
String s1 = "Holiday";
String s2 = "Home";
int x = s1.compareTo(s2);
System.out.println("Output: "+x);
Output : -1
The above code returns -1 as the first two characters are alike correspondingly while the 3rd characters are different and have a difference of -1 in their ASCII codes.
String s1 = "HOME";
String s2 = "Home";
int x = s1.compareTo(s2);
System.out.println("Output: "+x);
Output : -32
The above code returns -32 as the first characters of each string, are alike correspondingly while the 2nd characters are different being of different cases and have a difference of -32 in their ASCII codes.
String s1 = "Hometown";
String s2 = "Home";
int x = s1.compareTo(s2);
System.out.println("Output: "+x);
Output : 4
When compares Hometown completely contains the second string therefore the function returns the difference between the two string.
compareToIgnoreCase()
compareToIgnoreCase() method works exactly the same as the compareTo() method with the only difference being that this functions ignores any difference in cases of the two string in comparison.
String s1 = "HoMe";
String s2 = "hOmE";
int x = s1.compareTo(s2);
System.out.println("Output: "+x);
Output : 0
As the two strings are the same when the cases are ignored the functions finds no difference between the two strings and hence returns 0.