🔤 Strings & Text · Beginner

Core String methods in Java

length, charAt, substring, indexOf, contains, replace, trim/strip, isBlank, repeat.

🧩 The mystery"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 one

substring: 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"
🔮 Predict it

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));
  1. day Sun d
  2. day Sund d
  3. nday 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");   // true

Cleaning 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.

🔮 Predict it

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));
  1. false true x+y+z hahaha
  2. true true x+y-z hahaha
  3. false 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.

⚠️ The trap

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 the real world

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

  1. Indexes run from 0 to length() - 1
  2. substring(begin, end) includes begin, excludes end
  3. indexOf returns -1 when not found
  4. strip() understands Unicode spaces; trim() only ASCII ones
🤯 Did you know?

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"));
  1. 2 5 -1
  2. 3 6 0
  3. 2 5 0
  4. 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));
  1. Brew Java B
  2. aBrew JavaB B
  3. Brew JavaB r
  4. 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'.

Next: sort "apple" and "Zebra" and Java puts Zebra first. Is Java bad at the alphabet?