Java provides a lot of built-in methods for manipulating strings. These methods make it easy to perform common string operations like concatenation, splitting, and searching.
To find more code examples that demonstrate how to work with Strings in Java, check out the following tutorials:
- Java String
- Working With Strings in Java – Part 1
- Working With Strings in Java – Part 2
- StringBuilder class in Java
- StringBuffer class in Java
- StringBuilder vs StringBuffer in Java
Here are some of the most commonly used Java String methods:
length()
Returns the length of the string.
String str = "Hello World"; int len = str.length(); System.out.println(len); // output: 11
charAt(int index)
Returns the character at the specified index.
String str = "Hello World"; char ch = str.charAt(1); System.out.println(ch); // output: 'e'
substring(int beginIndex, int endIndex)
Returns a substring of the string.
String str = "Hello World"; String substr = str.substring(6, 11); System.out.println(substr); // output: 'World'
toLowerCase()
Converts the string to lowercase.
String str = "Hello World"; String lower = str.toLowerCase(); System.out.println(lower); // output: 'hello world'
toUpperCase()
Converts the string to uppercase.
String str = "Hello World"; String upper = str.toUpperCase(); System.out.println(upper); // output: 'HELLO WORLD'
indexOf(String str)
Returns the index of the first occurrence of the specified substring in the string.
String str = "Hello World"; int index = str.indexOf("World"); System.out.println(index); // output: 6
replace(char oldChar, char newChar)
Replaces all occurrences of the specified old char with the specified new char.
String str = "Hello World"; String newStr = str.replace('o', 'x'); System.out.println(newStr); // output: 'Hellx Wxrld'
startsWith(String prefix)
Returns true if the string starts with the specified prefix.
String str = "Hello World"; boolean startsWithHello = str.startsWith("Hello"); System.out.println(startsWithHello); // output: true
endsWith(String suffix)
Returns true if the string ends with the specified suffix.
String str = "Hello World"; boolean endsWithWorld = str.endsWith("World"); System.out.println(endsWithWorld); // output: true
split(String regex)
Splits the string into an array of substrings using the specified regular expression.
String str = "apple,banana,orange"; String[] fruits = str.split(","); for (String fruit : fruits) { System.out.println(fruit); } // output: // apple // banana // orange
trim()
Removes whitespace from both ends of the string.
String str = " Hello World "; String trimmedStr = str.trim(); System.out.println(trimmedStr); // output: 'Hello World'
concat(String str)
Concatenates the specified string to the end of the current string.
String str1 = "Hello"; String str2 = "World"; String result = str1.concat(" ").concat(str2); System.out.println(result); // output: 'Hello World'
isEmpty()
Returns true if the string is empty.
String str = ""; boolean isEmpty = str.isEmpty(); System.out.println(isEmpty); // output: true
equals(Object obj)
Compares the current string with the specified object to see if they are equal.
String str1 = "Hello"; String str2 = "World"; boolean areEqual = str1.equals(str2); System.out.println(areEqual); // output: false
equalsIgnoreCase(String str)
Compares the current string with the specified string, ignoring case.
String str1 = "HELLO"; String str2 = "hello"; boolean areEqual = str1.equalsIgnoreCase(str2); System.out.println(areEqual); // output: true
startsWith(String prefix, int offset)
Returns true if the substring starting at the specified offset of the current string starts with the specified prefix.
String str = "Hello World"; boolean startsWithWorld = str.startsWith("World", 6); System.out.println(startsWithWorld); // output: true
compareTo(String anotherString)
Compares the current string to the specified string lexicographically (i.e., in dictionary order).
String str1 = "apple"; String str2 = "banana"; int result = str1.compareTo(str2); System.out.println(result); // output: -1
These are just a few more examples of Java String methods that you can use to manipulate and process strings in your Java programs. With these methods, you can perform a wide range of operations on strings, including concatenation, searching, replacing, and more.