Theory
The librarian types like a human
Real input from the front desk this week:
" let us c " (stray spaces), "" (Enter pressed on nothing), "Programming in ANSI C by Balagurusamy" (needs just the title part).
BookBridge must clean, test, slice and search all of it. Java's String class ships a toolbox for exactly this, and your syllabus names 11 tools. Learn them as a family with 2 jobs: methods that inspect a string, and methods that produce a new one (never forgetting: immutable, always new).
Theory
First, learn to count like Java
Every method below speaks in indexes, starting at 0. Pin this example:
"Let Us C"
L e t ␣ U s ␣ C
0 1 2 3 4 5 6 7
So length() is 8 (the count), but the last index is 7 (count minus 1). charAt(4) is 'U'. Mixing up count and last index causes the StringIndexOutOfBoundsException half your batch will meet this week.
At a glance
The inspectors: ask, never change
| Method | Answers | On "Let Us C" |
|---|---|---|
| length() | How many characters? | 8 |
| charAt(4) | Which char sits at index 4? | 'U' |
| indexOf("s") | First index of this text, or -1 | 5 |
| lastIndexOf(" ") | Last index of this text, or -1 | 6 |
| isEmpty() | Is length() == 0? | false |
| equals(s) | Same characters? (last lesson) | content check |
At a glance
The producers: every result is a NEW string
| Method | Produces | Example |
|---|---|---|
| trim() | Copy without leading/trailing spaces | " let us c ".trim() → "let us c" |
| substring(4) | From index 4 to the end | "Let Us C" → "Us C" |
| substring(0, 3) | Indexes 0,1,2: end EXCLUSIVE | "Let Us C" → "Let" |
| concat(s) | This string + s joined | "Book".concat("Bridge") → "BookBridge" |
| split(" ") | String[] of the pieces | "Let Us C" → {"Let","Us","C"} |
| String.join("-", parts) | Pieces glued with a separator (static) | join("-","a","b") → "a-b" |
Practical
Cleaning one messy entry, end to end
public class CleanTitle {
public static void main(String[] args) {
String raw = " let us c ";
String t = raw.trim(); // "let us c"
System.out.println(t.length()); // 8
System.out.println(t.isEmpty()); // false
System.out.println(t.indexOf("us")); // 4
System.out.println(t.charAt(0)); // l
String[] words = t.split(" "); // {"let","us","c"}
System.out.println(words.length); // 3 (field, no brackets!)
System.out.println(String.join("-", words)); // let-us-c
}
}
Quiz
What does "Let Us C".substring(4, 6) return?
- "Us"
- "Us "
- " Us"
- "s C"
Show the answer
"Us"
substring(begin, end) takes indexes begin up to end EXCLUSIVE: indexes 4 and 5, the characters 'U' and 's', so "Us". Option B includes index 6 (the space), the classic off-by-one for students who read end as inclusive. A quick self-check that examiners themselves use: the result's length is always end minus begin, here 6 - 4 = 2 characters. If your answer has the wrong length, re-read the indexes.
Think first
The -1 sentinel
t.indexOf("Python") when t is "let us c": there is no Python anywhere in it. Before tapping: what comes back, and why that particular value?
Show the answer
-1. It cannot be 0 or any positive number, because every one of those is a real index where a match COULD sit; -1 is the one integer that can never be a position, so it safely means "not found". The working idiom everywhere in BookBridge: if (t.indexOf(q) != -1) { found }. Forgetting the check and using the -1 as an index in substring or charAt is an instant runtime exception.
Watch out
Three habits that lose marks
length() vs length: on a String it is a method with brackets, on an array a field without: t.length() but words.length. Exams plant both in one snippet, exactly like our listing.
Forgetting results are new strings: t.trim(); alone cleans nothing; assign it: t = t.trim();
charAt(t.length()) asks for index 8 of an 8-character string: StringIndexOutOfBoundsException. The last character lives at length() - 1.
Theory
Compose them like plumbing
The methods chain, because each producer returns a String ready for the next call: raw.trim().substring(0, 3).equals("let") reads as clean, slice, test. That composability is why these 11 cover most text work BookBridge will ever need. When the job becomes heavy repeated EDITING of one growing text, chaining new-string producers gets wasteful, and that is precisely the cue for the next lesson's StringBuffer.
Summary
Key takeaways
- Indexes start at 0; length() counts characters, last index is length() - 1.
- Inspectors: length, charAt, indexOf, lastIndexOf, isEmpty, equals.
- Producers (always a NEW string): trim, substring, concat, split, and static String.join.
- substring(begin, end) is end-exclusive; result length = end - begin.
- indexOf/lastIndexOf return -1 for not found: check before using.
- String length() has brackets; array length does not.
- Memory hook: inspectors ask, producers hand you a fresh copy.