Implementing
String Algorithms
Master Java's String methods, traverse characters with loops, and solve classic problems like searching, reversing, and substring extraction.
What Is a String in Java?
A String is an immutable sequence of characters. Unlike primitive types, Strings are objects: each character lives at a specific index starting from \( 0 \) up to \( \text{length} - 1 \).
Essential String Methods
Why String Algorithms Matter
String manipulation appears on nearly every AP exam. Understanding how to traverse, search, and build Strings is essential for both the multiple-choice and free-response sections.
Core Pattern: String Traversal
The fundamental pattern for processing every character in a String is a for loop from index \( 0 \) to \( \text{str.length()} - 1 \):
.equals() vs ==
This is the single most common source of bugs with Strings in Java. The two operations test fundamentally different things:
| .equals() | == | |
|---|---|---|
| Tests | Content equality | Reference equality |
| Meaning | Same characters? | Same object in memory? |
| Use for Strings? | ✓ Always | ✕ Never |
| Example | "Hi".equals(s) | "Hi" == s |
Understanding substring()
substring(a, b) returns characters from index a (inclusive) to index b (exclusive). Think of the indices as sitting between characters:
How to Solve String Problems
Example 1: Counting a Character
Problem: Count how many times the letter 'a' appears in a given String.
Example 2: Reversing a String
Problem: Return the reverse of a given String. Since Strings are immutable, we build a new one character by character.
Example 3: Counting Substring Occurrences
Problem: Count how many times a substring appears within a larger String (non-overlapping).
Example 4: Removing Characters
Problem: Remove all occurrences of a specific character from a String.
Common Mistakes
String Methods Cheat Sheet
| Method | Returns | Example |
|---|---|---|
| length() | int | "Cat".length() → 3 |
| charAt(i) | char | "Cat".charAt(1) → 'a' |
| substring(a, b) | String | "Cat".substring(0, 2) → "Ca" |
| indexOf(s) | int | "Cat".indexOf("at") → 1 |
| equals(s) | boolean | "Cat".equals("cat") → false |
| compareTo(s) | int | "A".compareTo("B") → < 0 |
Building Strings with Concatenation
Since Strings are immutable, each += creates a new String object. For the AP exam, this simple pattern is expected and acceptable:
Question 1 of 5
What is the value of "Hello".substring(1, 4)?
Question 2 of 5
Which correctly compares two String variables s1 and s2 for equal content?
Question 3 of 5
Consider the following method:
public static String mystery(String s) {
String result = "";
for (int i = s.length() - 1; i >= 0; i--) {
result += s.substring(i, i + 1);
}
return result;
}
What does mystery("Code") return?
Question 4 of 5
What is the value of "programming".indexOf("gram")?
Question 5 of 5
Consider the following code segment:
String s = "abcabc";
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.substring(i, i + 1).equals("b")) {
count++;
}
}
What is the value of count after the code executes?