Skip to main content

Command Palette

Search for a command to run...

All Java String Methods

Published
•6 min read•View as Markdown

Java’s String class is huge — it has over 60 methods in modern versions — so I’ll give you a complete, well-organized list of the most important ones, grouped by category, so it’s easy to study for interviews or coding.


1️⃣ Length, Empty Check, and Comparison

MethodDescription
length()Returns the number of characters in the string.
isEmpty()Returns true if the string has length 0.
isBlank() (Java 11+)Returns true if the string is empty or only whitespace.
equals(String other)Compares two strings for content equality.
equalsIgnoreCase(String other)Compares two strings ignoring case.
compareTo(String another)Lexicographically compares two strings.
compareToIgnoreCase(String another)Compares ignoring case.

2️⃣ Searching in Strings

MethodDescription
charAt(int index)Returns the character at given index.
indexOf(int ch)Returns index of first occurrence of character.
indexOf(String str)Returns index of first occurrence of substring.
indexOf(int ch, int fromIndex)Search from specific position.
lastIndexOf(int ch)Last occurrence of character.
lastIndexOf(String str)Last occurrence of substring.
contains(CharSequence s)Checks if substring exists.
startsWith(String prefix)Checks if string starts with given prefix.
startsWith(String prefix, int offset)Checks starting from a given index.
endsWith(String suffix)Checks if string ends with suffix.

3️⃣ Extracting Substrings

MethodDescription
substring(int beginIndex)Returns substring from given index to end.
substring(int beginIndex, int endIndex)Returns substring from begin to end-1.
subSequence(int beginIndex, int endIndex)Returns a CharSequence view.

4️⃣ Modifying / Creating New Strings

MethodDescription
concat(String str)Joins two strings.
replace(char oldChar, char newChar)Replaces all occurrences of a char.
replace(CharSequence target, CharSequence replacement)Replaces substring with another.
replaceAll(String regex, String replacement)Replaces using regex.
replaceFirst(String regex, String replacement)Replaces first match only.
repeat(int count) (Java 11+)Repeats string multiple times.

5️⃣ Trimming & Stripping

MethodDescription
trim()Removes leading & trailing spaces.
strip() (Java 11+)Removes Unicode whitespace from both ends.
stripLeading() (Java 11+)Removes leading whitespace.
stripTrailing() (Java 11+)Removes trailing whitespace.

6️⃣ Changing Case

MethodDescription
toLowerCase()Converts to lowercase using default locale.
toLowerCase(Locale locale)Converts with specific locale.
toUpperCase()Converts to uppercase using default locale.
toUpperCase(Locale locale)Converts with specific locale.

7️⃣ Splitting and Joining

MethodDescription
split(String regex)Splits string into array using regex.
split(String regex, int limit)Splits with a max limit.
(Static) String.join(CharSequence delimiter, CharSequence... elements)Joins multiple strings with delimiter.

8️⃣ Conversion Methods

MethodDescription
toCharArray()Converts to a char array.
getBytes()Converts to byte array (default charset).
getBytes(Charset charset)Converts using given charset.
String.valueOf(...) (static)Converts primitives/objects to string.
copyValueOf(char[] data) (static)Creates string from char array.
intern()Returns the canonical string from the string pool.

9️⃣ Checking Matches

MethodDescription
matches(String regex)Checks if matches regex.
regionMatches(...)Compares a region of strings.

🔟 Other Utility Methods

MethodDescription
format(String format, Object... args) (static)Returns formatted string.
indent(int n) (Java 12+)Adjusts indentation.
transform(Function<? super String, ? extends R> f) (Java 12+)Applies a transformation function.
translateEscapes() (Java 15+)Processes escape sequences like \n, \t.

I’ll make it organized by category, so it’s easy to follow and copy into your IDE.


javaCopyEditpublic class AllStringMethodsExample {
    public static void main(String[] args) {

        // 1️⃣ Length, Empty Check, and Comparison
        String s1 = "Hello";
        String s2 = "hello";
        System.out.println("length(): " + s1.length());
        System.out.println("isEmpty(): " + "".isEmpty());
        System.out.println("isBlank(): " + "   ".isBlank()); // Java 11+
        System.out.println("equals(): " + s1.equals(s2));
        System.out.println("equalsIgnoreCase(): " + s1.equalsIgnoreCase(s2));
        System.out.println("compareTo(): " + s1.compareTo(s2));
        System.out.println("compareToIgnoreCase(): " + s1.compareToIgnoreCase(s2));

        // 2️⃣ Searching
        String str = "Java Programming";
        System.out.println("charAt(2): " + str.charAt(2));
        System.out.println("indexOf('a'): " + str.indexOf('a'));
        System.out.println("indexOf(\"Pro\"): " + str.indexOf("Pro"));
        System.out.println("indexOf('a', 2): " + str.indexOf('a', 2));
        System.out.println("lastIndexOf('a'): " + str.lastIndexOf('a'));
        System.out.println("contains(\"Java\"): " + str.contains("Java"));
        System.out.println("startsWith(\"Java\"): " + str.startsWith("Java"));
        System.out.println("startsWith(\"Program\", 5): " + str.startsWith("Program", 5));
        System.out.println("endsWith(\"ing\"): " + str.endsWith("ing"));

        // 3️⃣ Substring extraction
        String subStr = "Automation";
        System.out.println("substring(5): " + subStr.substring(5));
        System.out.println("substring(0,4): " + subStr.substring(0, 4));
        System.out.println("subSequence(0,4): " + subStr.subSequence(0, 4));

        // 4️⃣ Modifying / Creating
        String modStr = "Hello World";
        System.out.println("concat(): " + modStr.concat("!!!"));
        System.out.println("replace(char,char): " + modStr.replace('l', 'x'));
        System.out.println("replace(seq,seq): " + modStr.replace("World", "Java"));
        System.out.println("replaceAll(): " + modStr.replaceAll("\\s", "_"));
        System.out.println("replaceFirst(): " + modStr.replaceFirst("l", "X"));
        System.out.println("repeat(3): " + "Hi ".repeat(3)); // Java 11+

        // 5️⃣ Trimming & Stripping
        String spaced = "   Hello Java   ";
        System.out.println("trim(): '" + spaced.trim() + "'");
        System.out.println("strip(): '" + spaced.strip() + "'"); // Java 11+
        System.out.println("stripLeading(): '" + spaced.stripLeading() + "'");
        System.out.println("stripTrailing(): '" + spaced.stripTrailing() + "'");

        // 6️⃣ Changing Case
        String caseStr = "Java";
        System.out.println("toLowerCase(): " + caseStr.toLowerCase());
        System.out.println("toUpperCase(): " + caseStr.toUpperCase());

        // 7️⃣ Splitting & Joining
        String csv = "apple,banana,orange";
        String[] fruits = csv.split(",");
        System.out.println("split():");
        for (String f : fruits) System.out.println(" - " + f);
        System.out.println("String.join(): " + String.join("-", "2025", "08", "13"));

        // 8️⃣ Conversion
        String convStr = "Hello";
        char[] chars = convStr.toCharArray();
        System.out.print("toCharArray(): ");
        for (char c : chars) System.out.print(c + " ");
        System.out.println();
        byte[] bytes = convStr.getBytes();
        System.out.println("getBytes(): " + bytes.length + " bytes");
        System.out.println("String.valueOf(): " + String.valueOf(123));
        char[] arr = {'J', 'a', 'v', 'a'};
        System.out.println("String.copyValueOf(): " + String.copyValueOf(arr));

        // 9️⃣ Matching
        String matchStr = "Java123";
        System.out.println("matches(): " + matchStr.matches("Java\\d+"));
        System.out.println("regionMatches(): " + matchStr.regionMatches(0, "JavaScript", 0, 4));

        // 🔟 Other Utilities
        String formatted = String.format("My name is %s, age %d", "Sam", 25);
        System.out.println("String.format(): " + formatted);
        String text = "Hello\\nWorld";
        System.out.println("translateEscapes(): " + text.translateEscapes()); // Java 15+
        String transformed = "java".transform(s -> s.toUpperCase() + " LANGUAGE"); // Java 12+
        System.out.println("transform(): " + transformed);
    }
}

How this helps you

  • Covers almost every important String method from Java 8 to Java 17.

  • Grouped logically (comparison, search, modify, trim, case, split/join, convert, match, utilities).

  • Can be run directly — no extra dependencies.

  • Output shows exact effect of each method, which helps in interviews.

More from this blog

Shreenibas Samal's blog

49 posts