String Methods
Common String Methods at a Glance
Method | Purpose | Example | Result |
.length() | Number of characters | "hello".length() | 5 |
.charAt(i) | Character at index i | "hello".charAt(1) | 'e' |
.substring(start, end) | Extract a portion of the string | "hello".substring(1, 4) | "ell" |
.indexOf(str) | Position of first match, or -1 | "hello".indexOf("l") | 2 |
.contains(str) | Whether str appears anywhere | "hello".contains("ell") | true |
.toUpperCase() | Convert to all uppercase | "hello".toUpperCase() | "HELLO" |
.toLowerCase() | Convert to all lowercase | "HELLO".toLowerCase() | "hello" |
.trim() | Remove leading/trailing ASCII whitespace | " hi ".trim() | "hi" |
.strip() | Remove leading/trailing whitespace (Unicode-aware) | " hi ".strip() | "hi" |
.split(regex) | Break into an array using a delimiter | "a,b,c".split(",") | ["a", "b", "c"] |
.replace(a, b) | Replace all occurrences of a with b | "cat".replace("c", "b") | "bat" |
.equals(other) | Compare content, case-sensitive | "Hi".equals("hi") | false |
.equalsIgnoreCase(other) | Compare content, ignoring case | "Hi".equalsIgnoreCase("hi") | true |
.isEmpty() | True if length is 0 | "".isEmpty() | true |
.isBlank() | True if empty or all whitespace | " ".isBlank() | true |
Extracting and Searching
length, charAt, substring, indexOf, contains
public class StringSearchDemo {
public static void main(String[] args) {
String text = "Hello, World!";
System.out.println(text.length()); // 13
System.out.println(text.charAt(7)); // 'W'
System.out.println(text.substring(7)); // "World!"
System.out.println(text.substring(7, 12)); // "World"
System.out.println(text.indexOf("World")); // 7
System.out.println(text.contains("World"));// true
System.out.println(text.indexOf("xyz")); // -1 (not found)
}
}13 W World! World 7 true -1
Changing Case and Whitespace
toUpperCase, toLowerCase, trim, strip
public class StringCleanupDemo {
public static void main(String[] args) {
String messy = " Java Rocks ";
System.out.println("[" + messy.trim() + "]");
System.out.println("[" + messy.strip() + "]");
System.out.println(messy.trim().toUpperCase());
System.out.println(messy.trim().toLowerCase());
}
}[Java Rocks] [Java Rocks] JAVA ROCKS java rocks
Splitting and Replacing
split and replace
public class StringTransformDemo {
public static void main(String[] args) {
String csv = "apple,banana,cherry";
String[] fruits = csv.split(",");
for (String fruit : fruits) {
System.out.println(fruit);
}
String sentence = "I like cats";
System.out.println(sentence.replace("cats", "dogs"));
}
}apple banana cherry I like dogs
Comparing and Checking for Emptiness
equals, equalsIgnoreCase, isEmpty, isBlank
public class StringChecksDemo {
public static void main(String[] args) {
String password = "Secret123";
System.out.println(password.equals("Secret123")); // true
System.out.println(password.equalsIgnoreCase("secret123"));// true
String empty = "";
String blank = " ";
System.out.println(empty.isEmpty()); // true
System.out.println(blank.isEmpty()); // false — it has spaces!
System.out.println(blank.isBlank()); // true — whitespace only
}
}true true true false true