Shiklo Programming
EN
  • English
  • Español
  • Português
  • Deutsch
  • Français
  • 日本語
  • हिन्दी

Java String Methods

The String class comes with dozens of methods. This topic shows the ones you will use again and again. Remember from Strings that a string never changes: each method returns a new string (or a number or a boolean), and the original stays as it was.

Changing case and removing spaces

public class Main {
    public static void main(String[] args) {
        String s = "  Hello Java  ";
        System.out.println(s.toUpperCase());
        System.out.println(s.toLowerCase());
        System.out.println("[" + s.strip() + "]");
        System.out.println("   ".isBlank());
        System.out.println("".isEmpty());
    }
}
Output
  HELLO JAVA  
  hello java  
[Hello Java]
true
true

strip() removes spaces (and tabs and new lines) at both ends; the older trim() does nearly the same. isBlank() is true when a string is empty or contains only spaces, which is useful to check user input.

Searching

indexOf gives the position where some text first appears, or -1 if it does not appear. contains, startsWith and endsWith answer yes-or-no questions:

public class Main {
    public static void main(String[] args) {
        String email = "asha.rao@example.com";
        System.out.println(email.indexOf("@"));
        System.out.println(email.indexOf("z"));
        System.out.println(email.contains("example"));
        System.out.println(email.startsWith("asha"));
        System.out.println(email.endsWith(".org"));
        System.out.println(email.lastIndexOf("."));
    }
}
Output
8
-1
true
true
false
16

Cutting out parts

substring(start, end) returns the characters from position start up to, but not including, position end. With one argument it goes to the end of the string. Together with indexOf it can split text at a known character:

public class Main {
    public static void main(String[] args) {
        String email = "asha.rao@example.com";
        int at = email.indexOf("@");
        String user = email.substring(0, at);
        String domain = email.substring(at + 1);
        System.out.println(user);
        System.out.println(domain);
    }
}
Output
asha.rao
example.com

Replacing, splitting and joining

public class Main {
    public static void main(String[] args) {
        String date = "2025-08-15";
        System.out.println(date.replace("-", "/"));
        String[] parts = date.split("-");
        System.out.println(parts[0]);
        System.out.println(parts.length);
        System.out.println(String.join(" | ", "red", "green", "blue"));
        System.out.println("ab".repeat(3));
    }
}
Output
2025/08/15
2025
3
red | green | blue
ababab

split cuts a string into an array of strings at every separator. Arrays are explained in the Arrays topic; for now, parts[0] is the first piece and parts.length the number of pieces. String.join does the opposite, and repeat repeats a string.

Chaining methods

Because every method returns a new string, you can call another method on the result straight away:

public class Main {
    public static void main(String[] args) {
        String input = "   mumbai   ";
        String clean = input.strip().toUpperCase().replace("U", "u");
        System.out.println(clean);
    }
}
Output
MuMBAI

Read a chain from left to right: strip the spaces, make it upper case, then replace each "U".

Practice exercises

  1. Store "Learning Java is fun" in a variable and print the position of "Java" and the text after it.
  2. Turn "apple,banana,cherry" into an array with split and print how many fruits there are.
  3. Print a line of 30 dashes with repeat.