Dark Mode

Master Java String Manipulation

A comprehensive reference guide to the most commonly used methods in Java's String class with practical examples and usage tips.

All Methods
Searching
Manipulation
Comparison
Conversion
Utility

indexOf()

str.indexOf(substring)
Returns: int
Searching

Returns the index within this string of the first occurrence of the specified substring.

View Example
String str = "Hello World"; int index = str.indexOf("World"); System.out.println(index); // Output: 6

lastIndexOf()

str.lastIndexOf(substring)
Returns: int
Searching

Returns the index within this string of the last occurrence of the specified substring.

View Example
String str = "apple, orange, apple"; int index = str.lastIndexOf("apple"); System.out.println(index); // Output: 14

substring()

str.substring(startIndex, endIndex)
Returns: String
Manipulation

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to endIndex - 1.

View Example
String str = "Hello World"; String sub = str.substring(6, 11); System.out.println(sub); // Output: World

replace()

str.replace(oldChar, newChar)
Returns: String
Manipulation

Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

View Example
String str = "Hello World"; String replaced = str.replace('o', 'x'); System.out.println(replaced); // Output: Hellx Wxrld

equals()

str.equals(anotherString)
Returns: boolean
Comparison

Compares this string to the specified object. The result is true if the argument is not null and is a String object that represents the same sequence of characters.

View Example
String str1 = "Hello"; String str2 = "hello"; boolean result1 = str1.equals(str2); boolean result2 = str1.equalsIgnoreCase(str2); System.out.println(result1); // false System.out.println(result2); // true

compareTo()

str.compareTo(anotherString)
Returns: int
Comparison

Compares two strings lexicographically. Returns a negative integer, zero, or a positive integer as this string is less than, equal to, or greater than the specified string.

View Example
String str1 = "apple"; String str2 = "banana"; int result = str1.compareTo(str2); System.out.println(result); // Output: -1 (since 'a' comes before 'b')

toLowerCase()

str.toLowerCase()
Returns: String
Conversion

Converts all of the characters in this String to lower case using the rules of the default locale.

View Example
String str = "Hello WORLD"; String lower = str.toLowerCase(); System.out.println(lower); // Output: hello world

toUpperCase()

str.toUpperCase()
Returns: String
Conversion

Converts all of the characters in this String to upper case using the rules of the default locale.

View Example
String str = "Hello World"; String upper = str.toUpperCase(); System.out.println(upper); // Output: HELLO WORLD

length()

str.length()
Returns: int
Utility

Returns the length of this string. The length is equal to the number of Unicode code units in the string.

View Example
String str = "Java"; int length = str.length(); System.out.println(length); // Output: 4

trim()

str.trim()
Returns: String
Utility

Returns a string whose value is this string, with any leading and trailing whitespace removed.

View Example
String str = " Hello World "; String trimmed = str.trim(); System.out.println("|" + trimmed + "|"); // Output: |Hello World|

contains()

str.contains(substring)
Returns: boolean
Searching

Returns true if and only if this string contains the specified sequence of char values.

View Example
String str = "Hello World"; boolean contains = str.contains("World"); System.out.println(contains); // Output: true

concat()

str.concat(string)
Returns: String
Manipulation

Concatenates the specified string to the end of this string.

View Example
String str1 = "Hello"; String str2 = " World"; String result = str1.concat(str2); System.out.println(result); // Output: Hello World

valueOf()

String.valueOf(object)
Returns: String
Conversion

Returns the string representation of the various data types. This is a static method.

View Example
int num = 42; double pi = 3.14159; String s1 = String.valueOf(num); String s2 = String.valueOf(pi); System.out.println(s1); // 42 System.out.println(s2); // 3.14159

isEmpty()

str.isEmpty()
Returns: boolean
Utility

Returns true if, and only if, length() is 0.

View Example
String str1 = ""; String str2 = "Hello"; System.out.println(str1.isEmpty()); // true System.out.println(str2.isEmpty()); // false