01
Overview

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.

02
Core Concept

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"
03
Key Vocabulary

Prefix + case vocabulary.

Used in command and tag detection.

Vocabulary
  • 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).
04
AP Exam Focus

How these methods are tested.

Reassignment after case conversion is the common trap.

Exam Focus
  • 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.
05
Worked Example

Strip "Mr. " or "Mrs. " from a name.

Prefix detection + substring + case normalization.

Worked Example AP-style reasoning

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.

06
Common Mistakes

Prefix + case pitfalls.

Most miss reassignment.

Watch Out
  • 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.
07
Reference Table

Prefix and case methods.

Each returns; none mutates.

Prefix & Case Reference

AP Quick Reference
MethodReturnsNote
startsWith(p)booleanCase-sensitive.
substring(n)StringFrom index n onward.
toUpperCase()StringNew copy.
toLowerCase()StringNew copy.
08
Practice Tip

How to master prefix + case.

Reassign after conversion; order checks longest-first.

Practice Strategy
  • 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.
09
Section · 09

Practice — attempt these now.

AP-style assessments aligned to this lesson. Time them.

FRQ Practice 30 min 16 marks Pending

AP CSA Unit 12 Topic 07 substring, startsWith, and Case Conversion FRQ Practice

AP-style topic practice assessment

Start