☰
The String class represents character strings. All string literals in Java, are implemented as instances of this class. Strings are constant; their values cannot be changed after they are created.
The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase.
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java.
The following are some of the important string functions:
length() function returns the length of the string.
String str="Java";
System.out.println(str.length());
Output : 4
charAt() function returns the character located at the specified index.
String str = "Java";
System.out.println(str.charAt(2));
Output : v
equalsIgnoreCase() compares two strings lexicographically, ignoring case differences.
String str = "Java";
System.out.println(str.equalsIgnoreCase("JAVA"));
Output : true
The java string toLowerCase() method returns the string in lowercase letter. In other words, it converts all characters of the string into lower case letter.
String str = "Java";
System.out.println(str.toLowerCase());
Output : java
Converts all of the characters in the String to upper case using the rules of the default locale.
String str = "Java";
System.out.println(str.toUpperCase());
Output : JAVA
This method returns a copy of the string, with leading and trailing whitespace omitted.
String str = " Java ";
System.out.println(str.trim());
Output : Java
replace() method replaces occurances of character with a specified new character.
String str = "Java";
System.out.println(str.replace('v','V));
Output : JaVa
The java string split() method splits this string against given regular expression and returns a String array.
String str = "Java";
String[] strArray = str.split("a");
for(String eachString:strArray){
System.out.println(eachString);
}
Output : J
v
Tests if the string starts with the specified prefix. It returns true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise.
String str = "Java";
System.out.println(str.startsWith("co"));
Output : false
Returns the index within this string of the first occurrence of the specified substring
String str = "Java";
System.out.println(str.indexOf("v"));
Output : 2
substring() method returns a part of the string. This method has two variants(overloaded methods).
public String substring(int beginIndex);
public String substring(int beginIndex, int endIndex);
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 = "CoreJavaGuru";
System.out.println(str.substring(4));
Output : JavaGuru
And for the other overloaded method:
System.out.println(str.substring(4,8));
Output : Java