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

Java String Methods

String methods are ready-made tools for text, like making letters big, cutting out pieces, or checking how a word starts. They give back new text instead of changing the old one.
Syntax
java
string.length();
string.substring(start, end);
string.equals(other);
string.toUpperCase();

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
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare text and set it to "Hello"
		String text = "Hello";
		// Print a line to the console
		System.out.println(text.toUpperCase());
		// Print a line to the console
		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
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare text and set it to "Hello World"
		String text = "Hello World";
		// Print a line to the console
		System.out.println(text.substring(6));
		// Print a line to the console
		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
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare file and set it to "photo.jpg"
		String file = "photo.jpg";
		// Print a line to the console
		System.out.println(file.startsWith("photo"));
		// Print a line to the console
		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
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare text and set it to "  Hello  "
		String text = "  Hello  ";
		// Print a line to the console
		System.out.println(text.trim());
		// Print a line to the console
		System.out.println("Hello".replace("l", "L"));
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Calling s.trim() or replace() and ignoring the result, since strings are immutable.
  2. Using == instead of equals() or equalsIgnoreCase().
  3. Using substring(start, end) and forgetting the end index is exclusive.
🔒

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.