The following are the most commonly used functions of the String
class:
The following line of code is an example of the charAt()
function in a Java program:
public char charAt(int index)
The charAt()
function returns the char value at the specified index. An index ranges from 0
to length() - 1
. The first char
value of the sequence is at index 0
, the next at index 1
, and so on, as for array indexing. The following is another example:
String str = "welcome"; System.out.println(str.charAt(2));
The output is displayed here:
l
The following line of code is an example of the equalsIgnoreCase()
function in a Java program. It compares one String
to another String
, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length, and corresponding characters in the two strings are equal ignoring case.
String str = "java"; System.out.println(str.equalsIgnoreCase("JAVA"));
The output will be as follows:
true
The following line of code is an example of the length*()
function in a Java program:
public int length()
The length()
function returns the length of this string. The length is equal to the number of Unicode code units in the string:
String str = "Welcome"; System.out.println(str.length());
The output will be as follows:
7
The following line of code is an example of the replace()
function in a Java program:
public String replaceAll(String regex, String replacement)
The replace()
function returns a new string resulting from replacing all occurrences of oldChar
in this string with newChar
:
String str = "Java programming"; System.out.println(str.replace('p','P'));
The output will be as follows:
Java Programming
The following line of code is an example of the substring()
function in a Java program:
public String substring(int beginIndex)
The substring()
function returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string:
String str = "Java Programming"; System.out.println(str.substring(5));
The output will be as follows:
Programming
The following line of code is another example of the substring()
function in a Java program:
public String substring(int beginIndex, int endIndex)
This returns a new string that is a substring of this string. The substring begins at the specified beginIndex
and extends to the character at the index endIndex - 1
. Thus, the length of the substring is endIndex-beginIndex
:
String str = "Java Programming"; System.out.println(str.substring(5,11));
The output will be as follows:
Program
The following line of code is an example of the toLowerCase()
function in a Java program:
public String toLowerCase()
The toLowerCase()
function converts all of the characters in this String
to lowercase using the rules of the default locale:
String str = "JAVA"; System.out.println(str.toLowerCase());
The output will be as follows:
java
The following line of code is an example of the toUpperCase()
function in a Java program:
public String toUpperCase()
The toUpperCase()
function converts all of the characters in this String
to uppercase using the rules of the default locale:
String str = "java"; System.out.println(str.toUpperCase());
The output will be as follows:
JAVA
The following line of code is an example of the trim()
function in a Java program:
public String trim()
The trim()
function returns a copy of the string, with leading and trailing whitespace omitted. This method can be used to trim whitespace from the beginning and end of a string:
String str = " Java "; System.out.println(str.trim());
The output will be as follows:
Java
Note
Following is a link on more information on String class methods:
https://docs.oracle.com/javase/6/docs/api/java/lang/String.html