Java String Methods

The following tutorials cover all the methods of java.lang.String class in Java, with syntax and well detailed examples.

In the following, each section contains link to the respective tutorial on the String method, a brief description about what the method does, example code snippet for the method, and the return value of the method.

String charAt()

String charAt() method takes an index (integer) value as argument, and returns the character in the string at the given index.

</>
Copy
String str = "Hello World";
char ch = str.charAt(6); // W

Returns value of char type.

String compareTo()

String compareTo() method takes a string value as argument, and compares the string with the given argument string lexicographically.

</>
Copy
String str1 = "Apple";
String str2 = "Apple";
int result = str1.compareTo(str2); // 0

Returns value of int type that represents if the string is equal to, less than, or greater than the given argument string.

String concat()

String concat() method concatenates or appends the given argument string to the end of the original string.

</>
Copy
String string1 = "Apple";
String string2 = "Banana";
String concatenatedString = string1.concat(string2); // "AppleBanana"

Returns String value created by concatenating the strings.

String contains()

String contains() method takes a string value as argument, and checks if the given argument string is present in the string.

</>
Copy
String str = "Hello World";
boolean result = str.contains("World"); // true

Returns boolean value of true if the string contains the given argument string. Or else, returns false.

String equals()

String equals() method takes an object as argument, and checks if the given argument is equal to the string.

</>
Copy
String str1 = "Apple";
String str2 = "Apple";
boolean result = str1.equals(str2); // true

Returns boolean value of true if the string is equal to the given object. Or else, returns false.

String equalsIgnoreCase()

String equalsIgnoreCase() method takes another string as argument, and checks if the given argument string is equal to the string while ignoring the case.

</>
Copy
String str1 = "Apple";
String str2 = "APPLE";
boolean result = str1.equalsIgnoreCase(str2); // true

Returns boolean value of true if the string is equal to the given another string, ignoring the case. Or else, returns false.

String indexOf()

String indexOf() method is used to find the index of first occurrence of given value in the string.

</>
Copy
String str = "Hello World.";
int index = str.indexOf("World");  // 6

Returns an int value representing the index of the given value in the string.

String isEmpty()

String isEmpty() method is used to check if the string is empty or not. The method returns true if the string is empty, or else it returns false.

</>
Copy
String str = "";
if (str.isEmpty()) { // true
  System.out.println("The string is EMPTY.");
}

Returns a boolean value representing whether the string is empty. If the string is empty, the return value is true, or else, the return value is false.

String lastIndexOf()

String lastIndexOf() method is used to find the index of last occurrence of given search value in the string.

</>
Copy
String str = "Hello World! Happy World.";
int index = str.lastIndexOf("World"); // 19

Returns an int value representing the index of last occurrence of the search value in the string.

String length()

String length() method returns the length of the string, which is the number of Unicode code points in the string, as an integer value.

</>
Copy
String str = "Hello World";
int strLength = str.length(); // 11

Returns an int value representing the number of Unicode code points, i.e., the length of the string.

String replace()

String replace() method is used to replace all the occurrences of an old character with a new character in the string, or replace all the occurrences of a target string with a new replacement string.

Replace character.

</>
Copy
String str = "ammle is good";
String result = str.replace('m', 'p');

Replace string.

</>
Copy
String str = "There are many apppleees in the apppleee basket.";
String result = str.replace("apppleee", "apple");

Returns a String value.

String split()

String split() method is used to split the given string by a delimiter into a string array of values.

</>
Copy
String str = "aba-cdc-abc";
String[] splits = str.split("-"); // [aba, cdc, abc]

Returns a String[] with the values from given string.

String startsWith()

String startsWith() method takes a prefix string as argument, and checks if the string starts with the given prefix string.

</>
Copy
String string = "Hello World";
if (string.startsWith("Hel")) { // true
	System.out.println("The string STARTS with given value.");
}

Returns a boolean value of true, if the string starts with the given value. Or else, returns false.

String substring()

String substring() method is used to find the substring of the string, where the substring is defined by a specific starting index and an optional ending index in the string.

</>
Copy
String str = "AppleBananaCherry";
String substr1 = str.substring(5); // BananaCherry
String substr2 = str.substring(5, 12); // BananaC

Returns a String value created by taking the characters in the given index range of the string.

String toLowerCase()

String toLowerCase() method converts all the characters in the string to lower case.

</>
Copy
String str = "Hello World";
String lowerCaseStr = str.toLowerCase(); // "hello world"

Returns a string value created from converting the characters in the string to lower case.

String toUpperCase()

String toUpperCase() method converts all the characters in the string to upper case.

</>
Copy
String str = "Hello World";
String upperCaseStr = str.toUpperCase(); // "HELLO WORLD"

Returns a string value created from converting the characters in the string to upper case.

String trim()

String trim() method returns a new string with the whitespace characters trimmed from the edges of the original string.

</>
Copy
String str = "   Hello Wolrd         ";
String trimmedStr = str.trim();  // "Hello Wolrd"

Returns a string value created from trimming the whitespaces from the edges of the string.