Java String Methods
In this page:
Changing String Case
toUpperCase() and toLowerCase() return a new String with every letter case-converted, leaving the original string object unchanged since strings can't be modified in place. This makes them convenient for case-insensitive comparisons when combined with equals().
Example: Changing String Case
public class Main {
public static void main(String[] args) {
String text = "Hello";
System.out.println(text.toUpperCase());
System.out.println(text.toLowerCase());
}
}
Login to try C/C++/Java/PHP code in the editor
Comparing Strings Correctly
Never compare String content with ==, since it checks whether two references point to the exact same object rather than whether their characters match — two strings built differently (e.g. via concatenation) can hold identical text but fail an == check. Use equals() for exact, case-sensitive comparison and equalsIgnoreCase() when case shouldn't matter.
Example: Comparing Strings Correctly
public class Main {
public static void main(String[] args) {
String a = new String("hello");
String b = new String("hello");
System.out.println(a == b); // false: different objects
System.out.println(a.equals(b)); // true: same content
}
}
Login to try C/C++/Java/PHP code in the editor
Extracting Substrings
substring(start) and substring(start, end) extract a portion of a String — the one-argument form takes everything from start to the end, while the two-argument form stops just before end (the end index is exclusive). Both use zero-based indices like the rest of Java's String API.
Example: Extracting Substrings
public class Main {
public static void main(String[] args) {
String text = "Hello World";
System.out.println(text.substring(6));
System.out.println(text.substring(0, 5));
}
}
Login to try C/C++/Java/PHP code in the editor
Checking Content Prefixes and Suffixes
startsWith() and endsWith() test whether a String begins or finishes with a given pattern, useful for filtering filenames by extension or checking a prefix. contains() checks more loosely, for a substring existing anywhere at all within the string.
Example: Checking Content Prefixes and Suffixes
public class Main {
public static void main(String[] args) {
String file = "photo.jpg";
System.out.println(file.startsWith("photo"));
System.out.println(file.endsWith(".jpg"));
System.out.println(file.contains("oto"));
}
}
Login to try C/C++/Java/PHP code in the editor
Trimming and Replacing
trim() strips leading and trailing whitespace (and does not touch whitespace in the middle of the string), which is essential when cleaning up user input that might have stray spaces. replace() swaps every occurrence of one character or substring with another throughout the whole string.
Example: Trimming and Replacing
public class Main {
public static void main(String[] args) {
String text = " Hello ";
System.out.println(text.trim());
System.out.println("Hello".replace("l", "L"));
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: