Most Important String Methods in Java
Fast recall
Quick revision
Core answer
length, charAt, substring, indexOf, contains, startsWith, endsWith, equals, case conversion, strip, replace, and split. Remember that indexes count UTF-16 code units, indexOf returns -1 when absent, split takes a regex, and methods never mutate the original String. Not every method returns a String: many return an int, char, boolean, array, or stream.Whether you know the practical String API well enough to use it in a coding problem without hesitation.
Forgetting that String methods return a new String — calling s.toUpperCase() without assigning the result is a no-op. Also confusing length() (method, needs parentheses) with array's length (field, no parentheses).
Mention that Java 11 added strip(), isBlank(), and lines() — strip() is Unicode-aware while trim() only removes ASCII whitespace (char <= 32).
Interview-ready guide
Interview answer
I'd group them rather than rattle off a list. For access there's length() and charAt(); for searching, indexOf(), contains(), and startsWith()/endsWith(); for comparing, equals() and equalsIgnoreCase(); for transforming, toUpperCase(), trim()/strip(), and replace(); and for extracting, substring() and split(). Conversion goes String.valueOf() one way and Integer.parseInt() the other.
The thing I always stress is that String is immutable, so String-transforming methods return a result without mutating the original; query methods return values such as int or boolean — writing s.toUpperCase(); on its own line changes nothing, you have to assign the result back. That's the bug I see most often. And length() is a method with parentheses, unlike an array's length field; that inconsistency catches people out both ways.
A detail that shows you've kept up with the language: Java 11 added strip(), isBlank(), and lines(). strip() is Unicode-aware where trim() only removes ASCII whitespace up to code 32 — so a non-breaking space survives trim() but not strip(). In modern code I default to strip(), and I check existence with indexOf() returning -1 before I ever use that result as an index.
Most Important String Methods in Java
Direct Answer
Core String methods include length, charAt, substring, indexOf, contains, startsWith, endsWith, equals, case conversion, strip, replace, and split. Remember that indexes count UTF-16 code units, indexOf returns -1 when absent, split takes a regex, and methods never mutate the original String. Not every method returns a String: many return an int, char, boolean, array, or stream.
Code Example
public class StringMethodsRef {
public static void main(String[] args) {
String s = " Hello, Java World! ";
// Length and access
System.out.println(s.length()); // 22 — total characters including spaces
System.out.println(s.charAt(2)); // H — index 0-based, includes leading spaces
System.out.println(s.isEmpty()); // false
System.out.println("".isEmpty()); // true
System.out.println(" ".isBlank()); // true — Java 11+, whitespace-only counts
// Substring — end is EXCLUSIVE
System.out.println(s.substring(2, 7)); // Hello
System.out.println(s.substring(9)); // Java World! (from index to end)
// Search
System.out.println(s.indexOf("Java")); // 9 — first occurrence, -1 if not found
System.out.println(s.contains("World")); // true
System.out.println(s.startsWith(" H")); // true
System.out.println(s.endsWith("! ")); // true
// Transform — always returns new String, never modifies s
System.out.println(s.toUpperCase()); // uppercase version
System.out.println(s.toLowerCase()); // lowercase version
System.out.println(s.trim()); // removes leading/trailing ASCII whitespace
System.out.println(s.strip()); // Java 11 — same but Unicode-aware
// Replace
System.out.println(s.replace("Java", "Python")); // replaces all occurrences
System.out.println(s.trim().replace(", ", " ")); // chains are fine
// Split
String csv = "a,b,c,d";
String[] parts = csv.split(",");
System.out.println(parts.length); // 4
System.out.println(parts[0]); // a
// Type conversion
String num = String.valueOf(42); // '42' — int to String
int n = Integer.parseInt("99"); // 99 — String to int
double d = Double.parseDouble("3.14"); // 3.14
}
}
Common Mistakes & Pitfalls
- Calling a transform without using the return value —
s.trim();on its own line does nothing, becauseStringis immutable. You must writes = s.trim();. This is the single most common String bug for beginners. - Writing
s.lengthinstead ofs.length()—Stringexposes a method, arrays expose a field. Mixing them up is a compile error, and it goes the other way too:arr.length()doesn't compile either. - Using
trim()on Unicode whitespace —trim()only removes characters with code≤ 32, so a non-breaking space (\u00A0) survives it and your "empty" check passes when it shouldn't. Usestrip()(Java 11+). - Feeding
indexOf()'s-1straight into another call —s.charAt(s.indexOf("x"))ors.substring(s.indexOf("x"))throwsStringIndexOutOfBoundsExceptionwhen the substring is absent. Always check for-1first. - Comparing text with
==instead ofequals()—==compares object references, not characters, so two strings with identical content can returnfalse.
Check your understanding
Answer these without looking back at the article.
- What is the difference between trim() and strip() in Java?
- How does String.split() work and what are the common pitfalls?
- What is the difference between replace() and replaceAll() in Java?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.