A user submits a search query: " PIZZA ". Before searching, you need to clean it up (trim spaces), normalize it (lowercase), then search your database. String operations make this possible.

Change case (normalize input)

Convert text to uppercase or lowercase for consistent comparisons.

example
Case.java
Replay: real traced execution (multi-file project)
public class Case {
    public static void main(String[] args) {
        String input = "Hello World";

        String upper = input.toUpperCase();
        String lower = input.toLowerCase();

        System.out.println("Original: " + input);
        System.out.println("Upper:    " + upper);
        System.out.println("Lower:    " + lower);

        // Practical: case-insensitive comparison
        String userAnswer = "YES";
        if (userAnswer.toLowerCase().equals("yes")) {
            System.out.println("User agreed (any case)");
        }
    }
}
public class Case {
    public static void main(String[] args) {
        String input = "JAVA programming";

        String upper = input.toUpperCase();
        String lower = input.toLowerCase();

        System.out.println("Original: " + input);
        System.out.println("Upper:    " + upper);
        System.out.println("Lower:    " + lower);

        // Practical: case-insensitive comparison
        String userAnswer = "YES";
        if (userAnswer.toLowerCase().equals("yes")) {
            System.out.println("User agreed (any case)");
        }
    }
}
public class Case {
    public static void main(String[] args) {
        String input = "PyThOn";

        String upper = input.toUpperCase();
        String lower = input.toLowerCase();

        System.out.println("Original: " + input);
        System.out.println("Upper:    " + upper);
        System.out.println("Lower:    " + lower);

        // Practical: case-insensitive comparison
        String userAnswer = "YES";
        if (userAnswer.toLowerCase().equals("yes")) {
            System.out.println("User agreed (any case)");
        }
    }
}
public class Case {
    public static void main(String[] args) {
        String input = "Hello World";

        String upper = input.toUpperCase();
        String lower = input.toLowerCase();

        System.out.println("Original: " + input);
        System.out.println("Upper:    " + upper);
        System.out.println("Lower:    " + lower);

        // Practical: case-insensitive comparison
        String userAnswer = "yes";
        if (userAnswer.toLowerCase().equals("yes")) {
            System.out.println("User agreed (any case)");
        }
    }
}
public class Case {
    public static void main(String[] args) {
        String input = "Hello World";

        String upper = input.toUpperCase();
        String lower = input.toLowerCase();

        System.out.println("Original: " + input);
        System.out.println("Upper:    " + upper);
        System.out.println("Lower:    " + lower);

        // Practical: case-insensitive comparison
        String userAnswer = "Yes";
        if (userAnswer.toLowerCase().equals("yes")) {
            System.out.println("User agreed (any case)");
        }
    }
}
  1. input ← Hello World, upper ← HELLO WORLD, lower ← hello world

    1public class Case {2    public static void main(String[] args) {3        String input→ Hello World = "Hello World";  //@input="JAVA programming", "PyThOn"4        5        String upper→ HELLO WORLD = input.toUpperCase();6        String lower→ hello world = input.toLowerCase();7        8        System.out.println("Original: " + inputHello World);9        System.out.println("Upper:    " + upperHELLO WORLD);10        System.out.println("Lower:    " + lowerhello world);11        12        // Practical: case-insensitive comparison13        String userAnswer→ YES = "YES";  //@userAnswer="yes", "Yes"14        if (userAnswer.toLowerCase().equals("yes")) {
    outputOriginal: Hello World
    Upper:    HELLO WORLD
    Lower:    hello world
  2. if (userAnswer.toLowerCase().equals("yes"))

    13String userAnswer = "YES";  //@userAnswer="yes", "Yes"14if (userAnswer.toLowerCase().equals("yes")) {15    System.out.println("User agreed (any case)");16}
    outputUser agreed (any case)
  1. input ← JAVA programming, upper ← JAVA PROGRAMMING, lower ← java programming

    1public class Case {2    public static void main(String[] args) {3        String input→ JAVA programming = "JAVA programming";4        5        String upper→ JAVA PROGRAMMING = input.toUpperCase();6        String lower→ java programming = input.toLowerCase();7        8        System.out.println("Original: " + inputJAVA programming);9        System.out.println("Upper:    " + upperJAVA PROGRAMMING);10        System.out.println("Lower:    " + lowerjava programming);11        12        // Practical: case-insensitive comparison13        String userAnswer→ YES = "YES";14        if (userAnswer.toLowerCase().equals("yes")) {
    outputOriginal: JAVA programming
    Upper:    JAVA PROGRAMMING
    Lower:    java programming
  2. if (userAnswer.toLowerCase().equals("yes"))

    13String userAnswer = "YES";14if (userAnswer.toLowerCase().equals("yes")) {15    System.out.println("User agreed (any case)");16}
    outputUser agreed (any case)
  1. input ← PyThOn, upper ← PYTHON, lower ← python, userAnswer ← YES

    1public class Case {2    public static void main(String[] args) {3        String input→ PyThOn = "PyThOn";4        5        String upper→ PYTHON = input.toUpperCase();6        String lower→ python = input.toLowerCase();7        8        System.out.println("Original: " + inputPyThOn);9        System.out.println("Upper:    " + upperPYTHON);10        System.out.println("Lower:    " + lowerpython);11        12        // Practical: case-insensitive comparison13        String userAnswer→ YES = "YES";14        if (userAnswer.toLowerCase().equals("yes")) {
    outputOriginal: PyThOn
    Upper:    PYTHON
    Lower:    python
  2. if (userAnswer.toLowerCase().equals("yes"))

    13String userAnswer = "YES";14if (userAnswer.toLowerCase().equals("yes")) {15    System.out.println("User agreed (any case)");16}
    outputUser agreed (any case)
  1. input ← Hello World, upper ← HELLO WORLD, lower ← hello world

    1public class Case {2    public static void main(String[] args) {3        String input→ Hello World = "Hello World";4        5        String upper→ HELLO WORLD = input.toUpperCase();6        String lower→ hello world = input.toLowerCase();7        8        System.out.println("Original: " + inputHello World);9        System.out.println("Upper:    " + upperHELLO WORLD);10        System.out.println("Lower:    " + lowerhello world);11        12        // Practical: case-insensitive comparison13        String userAnswer→ yes = "yes";14        if (userAnswer.toLowerCase().equals("yes")) {
    outputOriginal: Hello World
    Upper:    HELLO WORLD
    Lower:    hello world
  2. if (userAnswer.toLowerCase().equals("yes"))

    13String userAnswer = "yes";14if (userAnswer.toLowerCase().equals("yes")) {15    System.out.println("User agreed (any case)");16}
    outputUser agreed (any case)
  1. input ← Hello World, upper ← HELLO WORLD, lower ← hello world

    1public class Case {2    public static void main(String[] args) {3        String input→ Hello World = "Hello World";4        5        String upper→ HELLO WORLD = input.toUpperCase();6        String lower→ hello world = input.toLowerCase();7        8        System.out.println("Original: " + inputHello World);9        System.out.println("Upper:    " + upperHELLO WORLD);10        System.out.println("Lower:    " + lowerhello world);11        12        // Practical: case-insensitive comparison13        String userAnswer→ Yes = "Yes";14        if (userAnswer.toLowerCase().equals("yes")) {
    outputOriginal: Hello World
    Upper:    HELLO WORLD
    Lower:    hello world
  2. if (userAnswer.toLowerCase().equals("yes"))

    13String userAnswer = "Yes";14if (userAnswer.toLowerCase().equals("yes")) {15    System.out.println("User agreed (any case)");16}
    outputUser agreed (any case)

Normalize input before comparing - users might type "YES", "Yes", or "yes".

toUpperCase Convert to uppercase: `"Hello".toUpperCase()` → `"HELLO"`

Trim whitespace (clean input)

Remove extra spaces from the beginning and end of user input.

tabbed
Trim.java
Replay: real traced execution (multi-file project)
public class Trim {
    public static void main(String[] args) {
        String messyInput = "   Alice Smith   ";
        String clean = messyInput.trim();

        System.out.println("With spaces: '" + messyInput + "'");
        System.out.println("Trimmed:     '" + clean + "'");
        System.out.println("Length before: " + messyInput.length());
        System.out.println("Length after:  " + clean.length());

        // strip() is like trim() but handles Unicode whitespace
        String tabbed = "\t  data  \n";
        System.out.println("Strip: '" + tabbed.strip() + "'");
    }
}
public class Trim {
    public static void main(String[] args) {
        String messyInput = "   Alice Smith   ";
        String clean = messyInput.trim();

        System.out.println("With spaces: '" + messyInput + "'");
        System.out.println("Trimmed:     '" + clean + "'");
        System.out.println("Length before: " + messyInput.length());
        System.out.println("Length after:  " + clean.length());

        // strip() is like trim() but handles Unicode whitespace
        String tabbed = "\t\tmore\t\t";
        System.out.println("Strip: '" + tabbed.strip() + "'");
    }
}
  1. messyInput ← Alice Smith , clean ← Alice Smith, tabbed ← data

    1public class Trim {2    public static void main(String[] args) {3        String messyInput→    Alice Smith    = "   Alice Smith   ";4        String clean→ Alice Smith = messyInput.trim();5        6        System.out.println("With spaces: '" + messyInput   Alice Smith    + "'");7        System.out.println("Trimmed:     '" + cleanAlice Smith + "'");8        System.out.println("Length before: " + messyInput.length());9        System.out.println("Length after:  " + clean.length());10        11        // strip() is like trim() but handles Unicode whitespace12        String tabbed→ 	  data
     = "\t  data  \n";  //@tabbed="\t\tmore\t\t"13        System.out.println("Strip: '" + tabbed.strip() + "'");14    }
    outputWith spaces: '   Alice Smith   '
    Trimmed:     'Alice Smith'
    Length before: 17
    Length after:  11
    Strip: 'data'
  1. messyInput ← Alice Smith , clean ← Alice Smith, tabbed ← more

    1public class Trim {2    public static void main(String[] args) {3        String messyInput→    Alice Smith    = "   Alice Smith   ";4        String clean→ Alice Smith = messyInput.trim();5        6        System.out.println("With spaces: '" + messyInput   Alice Smith    + "'");7        System.out.println("Trimmed:     '" + cleanAlice Smith + "'");8        System.out.println("Length before: " + messyInput.length());9        System.out.println("Length after:  " + clean.length());10        11        // strip() is like trim() but handles Unicode whitespace12        String tabbed→ 		more		 = "\t\tmore\t\t";13        System.out.println("Strip: '" + tabbed.strip() + "'");14    }
    outputWith spaces: '   Alice Smith   '
    Trimmed:     'Alice Smith'
    Length before: 17
    Length after:  11
    Strip: 'more'

Users often accidentally add spaces. Always trim input before processing.

trim Remove leading/trailing whitespace: `" hi ".trim()` → `"hi"`

Find substring (search in text)

Check if a string contains another string, and where.

text
Find.java
Replay: real traced execution (multi-file project)
public class Find {
    public static void main(String[] args) {
        String text = "Hello World, Hello Java";

        // Find first occurrence
        int pos = text.indexOf("Hello");
        System.out.println("'Hello' first found at: " + pos);

        // Find from a position
        int secondPos = text.indexOf("Hello", pos + 1);
        System.out.println("'Hello' second found at: " + secondPos);

        // Check if not found
        int notFound = text.indexOf("Python");
        System.out.println("'Python' found at: " + notFound);  // -1

        // contains() is simpler for yes/no
        System.out.println("Contains 'World'? " + text.contains("World"));
        System.out.println("Contains 'Python'? " + text.contains("Python"));
    }
}
public class Find {
    public static void main(String[] args) {
        String text = "The quick brown fox";

        // Find first occurrence
        int pos = text.indexOf("Hello");
        System.out.println("'Hello' first found at: " + pos);

        // Find from a position
        int secondPos = text.indexOf("Hello", pos + 1);
        System.out.println("'Hello' second found at: " + secondPos);

        // Check if not found
        int notFound = text.indexOf("Python");
        System.out.println("'Python' found at: " + notFound);  // -1

        // contains() is simpler for yes/no
        System.out.println("Contains 'World'? " + text.contains("World"));
        System.out.println("Contains 'Python'? " + text.contains("Python"));
    }
}
  1. text ← Hello World, Hello Java, pos ← 0, secondPos ← 13, notFound ← -1

    1public class Find {2    public static void main(String[] args) {3        String text→ Hello World, Hello Java = "Hello World, Hello Java";  //@text="The quick brown fox"4        5        // Find first occurrence6        int pos→ 0 = text.indexOf("Hello");7        System.out.println("'Hello' first found at: " + pos0);8        9        // Find from a position10        int secondPos→ 13 = text.indexOf("Hello", pos0 + 1);11        System.out.println("'Hello' second found at: " + secondPos13);12        13        // Check if not found14        int notFound→ -1 = text.indexOf("Python");15        System.out.println("'Python' found at: " + notFound-1);  // -116        17        // contains() is simpler for yes/no18        System.out.println("Contains 'World'? " + text.contains("World"));19        System.out.println("Contains 'Python'? " + text.contains("Python"));20    }
    output'Hello' first found at: 0
    'Hello' second found at: 13
    'Python' found at: -1
    Contains 'World'? true
    Contains 'Python'? false
  1. text ← The quick brown fox, pos ← -1, secondPos ← -1, notFound ← -1

    1public class Find {2    public static void main(String[] args) {3        String text→ The quick brown fox = "The quick brown fox";4        5        // Find first occurrence6        int pos→ -1 = text.indexOf("Hello");7        System.out.println("'Hello' first found at: " + pos-1);8        9        // Find from a position10        int secondPos→ -1 = text.indexOf("Hello", pos-1 + 1);11        System.out.println("'Hello' second found at: " + secondPos-1);12        13        // Check if not found14        int notFound→ -1 = text.indexOf("Python");15        System.out.println("'Python' found at: " + notFound-1);  // -116        17        // contains() is simpler for yes/no18        System.out.println("Contains 'World'? " + text.contains("World"));19        System.out.println("Contains 'Python'? " + text.contains("Python"));20    }
    output'Hello' first found at: -1
    'Hello' second found at: -1
    'Python' found at: -1
    Contains 'World'? false
    Contains 'Python'? false

indexOf returns the position, or -1 if not found. contains is simpler for yes/no.

indexOf Find position: `"Hello".indexOf("l")` → `2`, returns `-1` if not found

Replace text

Substitute parts of a string with something else.

message
Replace.java
Replay: real traced execution (multi-file project)
public class Replace {
    public static void main(String[] args) {
        String original = "Hello World";

        // Replace all occurrences
        String replaced = original.replace("l", "L");
        System.out.println("Replace l→L: " + replaced);

        // Replace substring
        String newWorld = original.replace("World", "Java");
        System.out.println("Replace World→Java: " + newWorld);

        // Original unchanged (strings are immutable)
        System.out.println("Original still: " + original);

        // Practical: censor words
        String message = "This is bad and very bad";
        String censored = message.replace("bad", "***");
        System.out.println("Censored: " + censored);
    }
}
public class Replace {
    public static void main(String[] args) {
        String original = "Hello World";

        // Replace all occurrences
        String replaced = original.replace("l", "L");
        System.out.println("Replace l→L: " + replaced);

        // Replace substring
        String newWorld = original.replace("World", "Java");
        System.out.println("Replace World→Java: " + newWorld);

        // Original unchanged (strings are immutable)
        System.out.println("Original still: " + original);

        // Practical: censor words
        String message = "That was ugly behavior";
        String censored = message.replace("bad", "***");
        System.out.println("Censored: " + censored);
    }
}
  1. original ← Hello World, replaced ← HeLLo WorLd, newWorld ← Hello Java

    1public class Replace {2    public static void main(String[] args) {3        String original→ Hello World = "Hello World";4        5        // Replace all occurrences6        String replaced→ HeLLo WorLd = original.replace("l", "L");7        System.out.println("Replace l→L: " + replacedHeLLo WorLd);8        9        // Replace substring10        String newWorld→ Hello Java = original.replace("World", "Java");11        System.out.println("Replace World→Java: " + newWorldHello Java);12        13        // Original unchanged (strings are immutable)14        System.out.println("Original still: " + originalHello World);15        16        // Practical: censor words17        String message→ This is bad and very bad = "This is bad and very bad";  //@message="That was ugly behavior"18        String censored→ This is *** and very *** = message.replace("bad", "***");19        System.out.println("Censored: " + censoredThis is *** and very ***);20    }
    outputReplace l→L: HeLLo WorLd
    Replace World→Java: Hello Java
    Original still: Hello World
    Censored: This is *** and very ***
  1. original ← Hello World, replaced ← HeLLo WorLd, newWorld ← Hello Java

    1public class Replace {2    public static void main(String[] args) {3        String original→ Hello World = "Hello World";4        5        // Replace all occurrences6        String replaced→ HeLLo WorLd = original.replace("l", "L");7        System.out.println("Replace l→L: " + replacedHeLLo WorLd);8        9        // Replace substring10        String newWorld→ Hello Java = original.replace("World", "Java");11        System.out.println("Replace World→Java: " + newWorldHello Java);12        13        // Original unchanged (strings are immutable)14        System.out.println("Original still: " + originalHello World);15        16        // Practical: censor words17        String message→ That was ugly behavior = "That was ugly behavior";18        String censored→ That was ugly behavior = message.replace("bad", "***");19        System.out.println("Censored: " + censoredThat was ugly behavior);20    }
    outputReplace l→L: HeLLo WorLd
    Replace World→Java: Hello Java
    Original still: Hello World
    Censored: That was ugly behavior

replace creates a new string - the original is unchanged (strings are immutable).

replace Substitute text: `"Hello".replace("l", "L")` → `"HeLLo"`

Split string (parse CSV)

Break a string into parts using a delimiter.

csvLine
Split.java
Replay: real traced execution (multi-file project)
public class Split {
    public static void main(String[] args) {
        // Split CSV line
        String csvLine = "Alice,25,Engineer";
        String[] parts = csvLine.split(",");

        System.out.println("CSV: " + csvLine);
        System.out.println("Name: " + parts[0]);
        System.out.println("Age: " + parts[1]);
        System.out.println("Job: " + parts[2]);

        // Split by multiple spaces
        String sentence = "Hello   World   Java";
        String[] words = sentence.split("\\s+");
        System.out.println("Words: " + words.length);
        for (String word : words) {
            System.out.println("  - " + word);
        }

    }
}
public class Split {
    public static void main(String[] args) {
        // Split CSV line
        String csvLine = "Bob,30,Designer";
        String[] parts = csvLine.split(",");

        System.out.println("CSV: " + csvLine);
        System.out.println("Name: " + parts[0]);
        System.out.println("Age: " + parts[1]);
        System.out.println("Job: " + parts[2]);

        // Split by multiple spaces
        String sentence = "Hello   World   Java";
        String[] words = sentence.split("\\s+");
        System.out.println("Words: " + words.length);
        for (String word : words) {
            System.out.println("  - " + word);
        }

    }
}
  1. csvLine ← Alice,25,Engineer, sentence ← Hello World Java

    1public class Split {2    public static void main(String[] args) {3        // Split CSV line4        String csvLine→ Alice,25,Engineer = "Alice,25,Engineer";  //@csvLine="Bob,30,Designer"5        String[] parts = csvLine.split(",");6        7        System.out.println("CSV: " + csvLineAlice,25,Engineer);8        System.out.println("Name: " + parts[0]Alice);9        System.out.println("Age: " + parts[1]25);10        System.out.println("Job: " + parts[2]Engineer);11        12        // Split by multiple spaces13        String sentence→ Hello   World   Java = "Hello   World   Java";14        String[] words = sentence.split("\\s+");  //#?regex_split15        System.out.println("Words: " + words.length3);16        for (String word : words) {
    outputCSV: Alice,25,Engineer
    Name: Alice
    Age: 25
    Job: Engineer
    Words: 3
  2. for (String word : words)

    pass 1 of 3
    15System.out.println("Words: " + words.length);16for (String wordHello : words) {17    System.out.println("  - " + wordHello);18}
    output  - Hello
    All 3 passes — pass 1 is the card above
    password
    1Hello
    2World
    3Java
  1. csvLine ← Bob,30,Designer, sentence ← Hello World Java

    1public class Split {2    public static void main(String[] args) {3        // Split CSV line4        String csvLine→ Bob,30,Designer = "Bob,30,Designer";5        String[] parts = csvLine.split(",");6        7        System.out.println("CSV: " + csvLineBob,30,Designer);8        System.out.println("Name: " + parts[0]Bob);9        System.out.println("Age: " + parts[1]30);10        System.out.println("Job: " + parts[2]Designer);11        12        // Split by multiple spaces13        String sentence→ Hello   World   Java = "Hello   World   Java";14        String[] words = sentence.split("\\s+");15        System.out.println("Words: " + words.length3);16        for (String word : words) {
    outputCSV: Bob,30,Designer
    Name: Bob
    Age: 30
    Job: Designer
    Words: 3
  2. for (String word : words)

    pass 1 of 3
    15System.out.println("Words: " + words.length);16for (String wordHello : words) {17    System.out.println("  - " + wordHello);18}
    output  - Hello
    All 3 passes — pass 1 is the card above
    password
    1Hello
    2World
    3Java

Splitting is essential for parsing structured data like CSV files.

split Break apart: `"a,b,c".split(",")` → `["a", "b", "c"]`

Exercise: MoreOps.java

Explore more operations: substring, contains, startsWith, endsWith