Prefix detection plus case conversion.
Three methods that work together: startsWith for prefix check, substring for extraction, and case conversion for normalization.
startsWith(prefix) is a boolean test: does this String begin with the given prefix? substring(n) drops the first n characters. toUpperCase() and toLowerCase() create a new String in the requested case.
Together they handle prefix stripping, case-insensitive prefix detection, and normalization before comparison.
On the AP exam, these appear in FRQs about command parsing, tag detection, and word-prefix problems.
Detect prefix; strip prefix; convert case.
Three independent operations; combine as needed.
Prefix detection
word.startsWith("pre") // true if word begins with "pre"
Prefix stripping
public String stripPrefix(String word, String prefix) {
if (word.startsWith(prefix)) {
return word.substring(prefix.length());
}
return word;
}
If the prefix is present, return the rest; otherwise return the original.
Case-insensitive prefix detection
public boolean startsWithIgnoreCase(String word, String prefix) {
return word.toLowerCase().startsWith(prefix.toLowerCase());
}
Normalize both to lowercase before comparing.
Case conversion is non-destructive
toUpperCase and toLowerCase return new Strings — they don't modify the original.
String s = "Hello";
s.toUpperCase(); // "HELLO" returned but not stored
System.out.println(s); // still "Hello"
s = s.toUpperCase(); // reassign to capture
System.out.println(s); // "HELLO"
Prefix + case vocabulary.
Used in command and tag detection.
- Prefix — the beginning portion of a String.
- startsWith — boolean prefix test.
- endsWith — boolean suffix test.
- toUpperCase / toLowerCase — non-destructive case conversion.
- Normalization — converting to a uniform case for comparison.
- Strip — remove a portion (prefix or suffix).
How these methods are tested.
Reassignment after case conversion is the common trap.
- Case methods return new Strings. Must reassign to keep the change.
- startsWith is case-sensitive. Normalize first for case-insensitive checks.
- substring(n) skips the first n chars.
- Strings are immutable. No method modifies the original.
Strip "Mr. " or "Mrs. " from a name.
Prefix detection + substring + case normalization.
Problem. Remove the title from "Mr. Smith" or "Mrs. Jones" or "DR. Lee".
public String stripTitle(String name) {
String lower = name.toLowerCase();
if (lower.startsWith("mrs. ")) return name.substring(5);
if (lower.startsWith("mr. ")) return name.substring(4);
if (lower.startsWith("dr. ")) return name.substring(4);
return name;
}
Trace.
- "Mr. Smith" → lower starts with "mr. " → return name.substring(4) = "Smith".
- "Mrs. Jones" → lower starts with "mrs. " → return name.substring(5) = "Jones".
- "DR. Lee" → lower is "dr. lee" → starts with "dr. " → return name.substring(4) = "Lee".
- "Smith" → no title detected → return "Smith".
Note. The "mrs. " check must come before "mr. " — otherwise "Mrs. Jones" would match "mr. " first and strip too little.
Prefix + case pitfalls.
Most miss reassignment.
- Calling toLowerCase without assigning. Result is discarded.
- Wrong substring index. Must equal the prefix's length.
- Order of prefix checks. Longer prefixes must come first.
- Case-sensitive startsWith on user input. Misses uppercase variations.
Prefix and case methods.
Each returns; none mutates.
Prefix & Case Reference
AP Quick Reference| Method | Returns | Note |
|---|---|---|
startsWith(p) | boolean | Case-sensitive. |
substring(n) | String | From index n onward. |
toUpperCase() | String | New copy. |
toLowerCase() | String | New copy. |
How to master prefix + case.
Reassign after conversion; order checks longest-first.
- MCQ Practice — for each option, verify case-conversion results are assigned back.
- Java Lab — write stripTitle, isHttpsUrl. Order prefix checks longest first.
- FRQ Practice — normalize case before any case-insensitive comparison.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 12 Topic 07 substring, startsWith, and Case Conversion FRQ Practice
AP-style topic practice assessment