← Back to Core Java Course | Chapter 6: Strings | Lesson 2 of 5

Java String Methods

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

java
public class Main {
	public static void main(String[] args) {
		String text = "Hello";
		System.out.println(text.toUpperCase());
		System.out.println(text.toLowerCase());
	}
}

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

java
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
	}
}

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

java
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));
	}
}

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

java
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"));
	}
}

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

java
public class Main {
	public static void main(String[] args) {
		String text = "  Hello  ";
		System.out.println(text.trim());
		System.out.println("Hello".replace("l", "L"));
	}
}
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.