Core String methods in Java
length, charAt, substring, indexOf, contains, replace, trim/strip, isBlank, repeat.
"JavaBrew".substring(0, 4) gives "Java" — four letters — even though you asked for index 4. Off by one? No: once you see how Java counts, it all clicks.A row of numbered boxes
A String is a row of characters, each in a numbered box. Numbering starts at 0, so the last box is length() - 1. charAt(i) reads box i.
String s = "hello"; // h0 e1 l2 l3 o4
s.length(); // 5
s.charAt(1); // 'e'
s.charAt(4); // 'o', the last onesubstring: start in, end out
substring(begin, end) **includes begin and excludes end** — so the result has end - begin characters. substring(begin) runs to the end of the String.
String s = "JavaBrew";
s.substring(0, 4); // "Java" (0..3)
s.substring(4); // "Brew"Slice it
What does this print?
String s = "Sunday";
System.out.println(s.substring(3));
System.out.println(s.substring(0, 3));
System.out.println(s.charAt(3));day Sun dday Sund dnday Sun a
Show the answer
day, Sun, d. Index 3 is 'd'. substring(3) starts there; substring(0, 3) stops just before it, because the end index is exclusive.
Searching
indexOf finds the first match, lastIndexOf the last. When nothing is found they return -1 — never 0, because 0 is a real index. contains just answers true/false.
String s = "banana";
s.indexOf('n'); // 2
s.lastIndexOf('a'); // 5
s.indexOf("x"); // -1
s.contains("nan"); // trueCleaning and building
replace swaps every match, not just the first. trim() strips spaces at both ends but only ASCII ones (up to U+0020); strip() (Java 11) understands all Unicode whitespace. isEmpty() means length 0; isBlank() (Java 11) also accepts whitespace-only text. repeat(n) copies the text n times.
Blank or empty?
What does this print?
System.out.println(" ".isEmpty());
System.out.println(" ".isBlank());
System.out.println("x-y-z".replace("-", "+"));
System.out.println("ha".repeat(3));false true x+y+z hahahatrue true x+y-z hahahafalse true x+y-z ha3
Show the answer
Two spaces have length 2, so not empty — but they're blank. replace changed both dashes, and repeat(3) gives hahaha.
Walking off the end
The end index may be **at most length()**. Ask for more and Java doesn't quietly cut it short — it throws StringIndexOutOfBoundsException.
String s = "abc"; // length 3
s.substring(1, 3); // "bc" fine
s.substring(1, 4); // throws!In real projects
Parsing is everywhere: file extensions with lastIndexOf('.'), usernames with strip(), "is this field filled in?" with isBlank(). Forgetting the -1 check (name.substring(name.indexOf('@')) on a string without @) is a classic crash in production logs.
Key takeaways
- Indexes run from 0 to length() - 1
- substring(begin, end) includes begin, excludes end
- indexOf returns -1 when not found
- strip() understands Unicode spaces; trim() only ASCII ones
Since Java 9, a String containing only Latin-1 characters stores 1 byte per character instead of 2 ("Compact Strings", JEP 254). Your code never notices: length() and charAt behave exactly the same.
Practice questions
What does this print?
String s = "banana";
System.out.println(s.indexOf('n'));
System.out.println(s.lastIndexOf('a'));
System.out.println(s.indexOf("x"));- 2 5 -1
- 3 6 0
- 2 5 0
- 1 5 -1
Check your answer
2 5 -1. b=0, a=1, n=2, so the first 'n' is at 2. The last 'a' is at 5. A missing value gives -1, never 0, because 0 is a real index.
What does this print?
String s = "JavaBrew";
System.out.println(s.substring(4));
System.out.println(s.substring(0, 4));
System.out.println(s.charAt(4));- Brew Java B
- aBrew JavaB B
- Brew JavaB r
- Brew Java r
Check your answer
Brew Java B. substring(4) starts at index 4 ('B'). substring(0, 4) takes indexes 0 to 3 because the end is exclusive. charAt(4) is 'B'.