String Methods
Common methods: length(), charAt(), substring(), etc.
Interview Relevant: String manipulation in coding tests
8 min read
Essential String Methods
The String class provides numerous methods for manipulating and querying strings. Here are the most important ones categorized by function.
Method Categories
| Category | Methods |
|---|---|
| Info | length(), isEmpty(), isBlank() |
| Access | charAt(), substring(), toCharArray() |
| Search | indexOf(), lastIndexOf(), contains() |
| Transform | toUpperCase(), toLowerCase(), trim(), strip() |
| Modify | replace(), replaceAll(), split(), join() |
💡 Remember: All String methods return NEW strings - the original is never modified!
Code Examples
Info, access, and search methods
java
1// Information methods
2String str = " Hello World ";
3
4str.length(); // 15 (includes spaces)
5str.isEmpty(); // false
6str.isBlank(); // false (Java 11+)
7"".isEmpty(); // true
8" ".isBlank(); // true (Java 11+)
9
10// Access methods
11str.charAt(2); // 'H' (after spaces)
12str.substring(2, 7); // "Hello"
13str.substring(8); // "World "
14str.toCharArray(); // char[] {'H', 'e', 'l', 'l', 'o', ...}
15
16// Search methods
17str.indexOf('o'); // 6 (first occurrence)
18str.indexOf('o', 7); // 9 (search from index 7)
19str.lastIndexOf('o'); // 9
20str.indexOf("World"); // 8
21str.contains("World"); // true
22str.startsWith(" H"); // true
23str.endsWith(" "); // trueTransformation and replacement methods
java
1// Transformation methods
2String str = " Hello World ";
3
4str.toUpperCase(); // " HELLO WORLD "
5str.toLowerCase(); // " hello world "
6
7// Trimming whitespace
8str.trim(); // "Hello World" (removes leading/trailing)
9str.strip(); // "Hello World" (Java 11+, Unicode aware)
10str.stripLeading(); // "Hello World " (Java 11+)
11str.stripTrailing(); // " Hello World" (Java 11+)
12
13// Replace methods
14String text = "Hello, Hello!";
15text.replace('H', 'J'); // "Jello, Jello!"
16text.replace("Hello", "Hi"); // "Hi, Hi!"
17text.replaceFirst("Hello", "Hi"); // "Hi, Hello!"
18text.replaceAll("[aeiou]", "*"); // "H*ll*, H*ll*!" (regex)
19
20// Repeat (Java 11+)
21"Ha".repeat(3); // "HaHaHa"Split, join, and format methods
java
1// Split and Join
2String csv = "apple,banana,cherry";
3
4// Split into array
5String[] fruits = csv.split(",");
6// ["apple", "banana", "cherry"]
7
8// Split with limit
9"a:b:c:d".split(":", 2); // ["a", "b:c:d"]
10
11// Join array into string
12String joined = String.join(", ", fruits);
13// "apple, banana, cherry"
14
15// Join with stream
16String fromList = String.join("-", Arrays.asList("A", "B", "C"));
17// "A-B-C"
18
19// Format methods
20String.format("Name: %s, Age: %d", "Alice", 25);
21// "Name: Alice, Age: 25"
22
23String.format("Price: $%.2f", 19.99);
24// "Price: $19.99"
25
26// Formatted (Java 15+)
27"Hello %s!".formatted("World");
28// "Hello World!"Modern String methods (Java 11+)
java
1// Java 11+ String methods
2String text = " Java Programming ";
3
4// strip() is Unicode-aware (better than trim())
5text.strip(); // "Java Programming"
6
7// Check if blank
8" \t\n".isBlank(); // true
9
10// Lines (splits by line terminators)
11String multiline = "Line1\nLine2\nLine3";
12multiline.lines().forEach(System.out::println);
13
14// Repeat
15"=".repeat(20); // "===================="
16
17// Java 12+ methods
18String indented = "Hello".indent(4); // " Hello\n"
19
20// Transform (Java 12+)
21String result = "hello"
22 .transform(String::toUpperCase)
23 .transform(s -> s + "!");
24// "HELLO!"
25
26// Java 15+ methods
27String stripped = " text ".stripIndent(); // For text blocksUse Cases
- Parsing and tokenizing input
- Text searching and filtering
- Data cleaning and normalization
- Building formatted output
- Configuration parsing
- Log processing
Common Mistakes to Avoid
- Forgetting that methods return new strings
- Using split() without escaping regex chars
- Off-by-one errors with substring()
- NullPointerException when calling methods on null
- Using replaceAll() when replace() would work
- Not handling empty string edge cases