JavaString Methods

String Methods

The String class comes with a large toolkit of methods for inspecting and transforming text. This page walks through the ones you'll reach for constantly, with worked examples of the most important behaviors.
Common String Methods at a Glance

Method

Purpose

Example

Result

.length()

Number of characters

"hello".length()

5

.charAt(i)

Character at index i

"hello".charAt(1)

'e'

.substring(start, end)

Extract a portion of the string

"hello".substring(1, 4)

"ell"

.indexOf(str)

Position of first match, or -1

"hello".indexOf("l")

2

.contains(str)

Whether str appears anywhere

"hello".contains("ell")

true

.toUpperCase()

Convert to all uppercase

"hello".toUpperCase()

"HELLO"

.toLowerCase()

Convert to all lowercase

"HELLO".toLowerCase()

"hello"

.trim()

Remove leading/trailing ASCII whitespace

" hi ".trim()

"hi"

.strip()

Remove leading/trailing whitespace (Unicode-aware)

" hi ".strip()

"hi"

.split(regex)

Break into an array using a delimiter

"a,b,c".split(",")

["a", "b", "c"]

.replace(a, b)

Replace all occurrences of a with b

"cat".replace("c", "b")

"bat"

.equals(other)

Compare content, case-sensitive

"Hi".equals("hi")

false

.equalsIgnoreCase(other)

Compare content, ignoring case

"Hi".equalsIgnoreCase("hi")

true

.isEmpty()

True if length is 0

"".isEmpty()

true

.isBlank()

True if empty or all whitespace

" ".isBlank()

true

Extracting and Searching

length, charAt, substring, indexOf, contains

Java
public class StringSearchDemo {
    public static void main(String[] args) {
        String text = "Hello, World!";

        System.out.println(text.length());        // 13
        System.out.println(text.charAt(7));        // 'W'
        System.out.println(text.substring(7));     // "World!"
        System.out.println(text.substring(7, 12)); // "World"
        System.out.println(text.indexOf("World")); // 7
        System.out.println(text.contains("World"));// true
        System.out.println(text.indexOf("xyz"));   // -1 (not found)
    }
}
13
W
World!
World
7
true
-1
Note
.substring(start) takes everything from start to the end. .substring(start, end) takes characters from start up to but not including end. .indexOf() returns -1, not an exception, when nothing matches — always check for that before using the result.
Changing Case and Whitespace

toUpperCase, toLowerCase, trim, strip

Java
public class StringCleanupDemo {
    public static void main(String[] args) {
        String messy = "  Java Rocks  ";

        System.out.println("[" + messy.trim() + "]");
        System.out.println("[" + messy.strip() + "]");
        System.out.println(messy.trim().toUpperCase());
        System.out.println(messy.trim().toLowerCase());
    }
}
[Java Rocks]
[Java Rocks]
JAVA ROCKS
java rocks
Tip
Prefer .strip() over .trim() in modern Java. .strip() understands full Unicode whitespace, while .trim() only recognizes a narrow set of ASCII whitespace characters.
Splitting and Replacing

split and replace

Java
public class StringTransformDemo {
    public static void main(String[] args) {
        String csv = "apple,banana,cherry";
        String[] fruits = csv.split(",");

        for (String fruit : fruits) {
            System.out.println(fruit);
        }

        String sentence = "I like cats";
        System.out.println(sentence.replace("cats", "dogs"));
    }
}
apple
banana
cherry
I like dogs
Comparing and Checking for Emptiness

equals, equalsIgnoreCase, isEmpty, isBlank

Java
public class StringChecksDemo {
    public static void main(String[] args) {
        String password = "Secret123";

        System.out.println(password.equals("Secret123"));         // true
        System.out.println(password.equalsIgnoreCase("secret123"));// true

        String empty = "";
        String blank = "   ";

        System.out.println(empty.isEmpty()); // true
        System.out.println(blank.isEmpty()); // false — it has spaces!
        System.out.println(blank.isBlank()); // true — whitespace only
    }
}
true
true
true
false
true
Note
Every method in this table returns a new String (or array, or boolean) — none of them modify the original string in place. This is a direct consequence of String immutability: since a String can never change, the only way a method can give you a transformed version is to hand back a brand new object.