Aral Code

Basics Lesson 7 of 21

Java Strings and String Methods

Work with text in Java — length, charAt, substring, indexOf, toUpperCase, trim, replace, split, contains, comparing with equals and equalsIgnoreCase, and StringBuilder.

A String holds text. Because String is a class, every string comes with dozens of useful methods. You'll use them constantly to clean up input, build messages and check data.

Creating strings

String school = "Rizal National High School";
String empty = "";
String full = "Juan" + " " + "Dela Cruz";

Common methods

public class Main {
    public static void main(String[] args) {
        String name = "  Maria Clara  ";
        String clean = name.trim();

        System.out.println(clean.length());
        System.out.println(clean.toUpperCase());
        System.out.println(clean.toLowerCase());
        System.out.println(clean.charAt(0));
        System.out.println(clean.indexOf("Clara"));
        System.out.println(clean.substring(0, 5));
        System.out.println(clean.substring(6));
        System.out.println(clean.replace("a", "@"));
        System.out.println(clean.contains("Clara"));
        System.out.println(clean.startsWith("Mar"));
        System.out.println(clean.isEmpty());
    }
}

Output

11
MARIA CLARA
maria clara
M
6
Maria
Clara
M@ri@ Cl@r@
true
true
false
MethodReturns
length()Number of characters
charAt(i)The character at index i (starts at 0)
substring(a, b)Characters from index a up to, not including, b
indexOf(x)Position of x, or -1 if not found
toUpperCase() / toLowerCase()Changed case
trim() / strip()Without spaces at both ends
replace(a, b)All a replaced with b
contains(x), startsWith(x), endsWith(x)true/false
split(x)An array of parts

Strings are immutable: methods never change the original string; they return a new one. To keep the result, assign it: name = name.trim();.

Comparing strings: equals, not ==

public class Main {
    public static void main(String[] args) {
        String a = "yes";
        String b = new String("yes");

        System.out.println(a == b);                 // compares objects
        System.out.println(a.equals(b));            // compares text
        System.out.println("YES".equals(a));
        System.out.println("YES".equalsIgnoreCase(a));
        System.out.println("apple".compareTo("banana"));
    }
}

Output

false
true
false
true
-1

compareTo returns a negative number if the first string comes first alphabetically, 0 if equal, positive if it comes after. It's used for sorting.

split

public class Main {
    public static void main(String[] args) {
        String subjects = "Math,Science,English,Filipino";
        String[] parts = subjects.split(",");
        System.out.println(parts.length + " subjects");
        for (String s : parts) {
            System.out.println("- " + s);
        }
    }
}

Output

4 subjects
- Math
- Science
- English
- Filipino

Looping through characters

public class Main {
    public static void main(String[] args) {
        String word = "Pilipinas";
        int vowels = 0;
        for (int i = 0; i < word.length(); i++) {
            char c = Character.toLowerCase(word.charAt(i));
            if ("aeiou".indexOf(c) >= 0) {
                vowels++;
            }
        }
        System.out.println(word + " has " + vowels + " vowels.");
    }
}

Output

Pilipinas has 4 vowels.

StringBuilder: building long text

Because strings are immutable, joining many strings in a loop creates many temporary objects. StringBuilder is a changeable text buffer that's much faster for this:

public class Main {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= 5; i++) {
            sb.append(i);
            if (i < 5) sb.append(" - ");
        }
        System.out.println(sb.toString());
        System.out.println(new StringBuilder("Rizal").reverse());
    }
}

Output

1 - 2 - 3 - 4 - 5
laziR

Text blocks

Since Java 15, triple quotes create multi-line strings easily:

String menu = """
    1. Pandesal
    2. Kape
    3. Itlog
    """;

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What does "Java".charAt(1) return?
2. What does "Pilipinas".substring(0, 4) return?
3. Which correctly checks if answer is "yes", ignoring case?
4. Why use StringBuilder in loops?

By Sir Reyel · Last updated 2026-12-04