AP CSA String Methods
String Methods in AP CSA: Complete Guide (2025-2026)
String methods in AP CSA are tested on every AP Computer Science A exam — the AP Java Quick Reference lists exactly which ones you are responsible for, and each appears in MCQ trace questions and FRQ string-processing problems in Unit 1 (15–25%). The core methods are substring(), indexOf(), compareTo(), equals(), length(), charAt(), and toUpperCase()/toLowerCase(). Strings in Java are immutable — every method returns a new String and never modifies the original, which is itself a major AP exam trap.
📄 Table of Contents
💻 Code Examples — Predict First
Before running each example, write down your prediction. This is the single most effective AP exam study technique.
🤔 Predict the output before running:
public class Main {
public static void main(String[] args) {
String s = "hello world";
System.out.println(s.length()); // 11
System.out.println(s.charAt(4)); // 'o'
System.out.println(s.substring(6)); // "world"
System.out.println(s.substring(0, 5)); // "hello"
System.out.println(s.indexOf("o")); // 4
System.out.println(s.indexOf("o", 5)); // 7
}
}
11 / o / world / hello / 4 / 7 — substring(6) returns from index 6 to end. substring(0,5) returns indices 0–4 (end exclusive). indexOf("o") finds first 'o' at 4. indexOf("o",5) searches starting at index 5, finds 'o' at 7.
🤔 Predict the output before running:
public class Main {
public static void main(String[] args) {
String a = "apple";
String b = "banana";
String c = "apple";
System.out.println(a.equals(c)); // true
System.out.println(a == c); // unreliable
System.out.println(a.compareTo(b)); // negative (a < b)
System.out.println(b.compareTo(a)); // positive (b > a)
System.out.println(a.compareTo(c)); // 0 (equal)
}
}
true / (unreliable) / negative / positive / 0 — Always use .equals() for String content comparison. compareTo() returns negative if caller comes first alphabetically, positive if caller comes second, 0 if equal.
🤔 Predict the output before running:
public class Main {
public static void main(String[] args) {
String s = "Java Programming";
System.out.println(s.toUpperCase());
System.out.println(s.toLowerCase());
System.out.println(s.contains("Java")); // true
// Strings are IMMUTABLE
s.toUpperCase(); // no effect on s
System.out.println(s); // unchanged
}
}
JAVA PROGRAMMING / java programming / true / Java Programming — toUpperCase() and toLowerCase() return NEW Strings. The original s is unchanged because Strings are immutable. You must reassign: s = s.toUpperCase().
❌ Common Pitfalls
These are the mistakes students most often make on the AP CSA exam with String methods AP CSA substring indexOf compareTo. Study them carefully.
== checks if two String variables reference the same object in memory, not if they have the same content. Use .equals() for content comparison. This is the #1 most common String bug in AP CSA.
s1 == s2 // WRONG: reference equality s1.equals(s2) // CORRECT: content equality
substring(a, b) returns characters from index a up to but NOT including index b. The result has length b - a. Students often expect the character at index b to be included.
"abcde".substring(1, 4) // "bcd" (NOT "bcde") // Length = 4 - 1 = 3
Strings are immutable in Java. Methods like toUpperCase(), toLowerCase(), and substring() return a new String and leave the original unchanged. You must reassign the result to use it.
s = "hello"; s.toUpperCase(); // does nothing useful s = s.toUpperCase(); // CORRECT: reassign result
a.compareTo(b) returns negative if a comes before b alphabetically, positive if a comes after b, 0 if equal. Students often confuse which sign means which ordering.
"apple".compareTo("banana") // negative (a < b alphabetically)
"zebra".compareTo("ant") // positive (z > a alphabetically)
On the AP exam, String method questions almost always test one of three things: (1) the exact indices for substring (remember: end is exclusive), (2) equals vs == for comparison, (3) the return value of compareTo. Predict the output before looking at the answer choices.
indexOf() returns -1 if the search string is not found. This is a common AP MCQ trap: code that uses the return value of indexOf in an array index or arithmetic will produce unexpected results when the substring doesn’t exist.
✍ Check for Understanding (8 Questions)
"computer".substring(3, 6) return?
"hello".indexOf("l") return?
String s = "AP CSA";
s.toLowerCase();
System.out.println(s);
"banana".compareTo("apple") return?
"hello".substring(1, 4)?
"search".indexOf("x") return?
❓ Frequently Asked Questions
The AP Java Quick Reference lists: int length(), String substring(int from), String substring(int from, int to), int indexOf(String str), boolean equals(Object other), int compareTo(String other), and String toUpperCase()/toLowerCase(). charAt() and contains() are also commonly tested.
substring(a) returns from index a to the end of the string. substring(a,b) returns from index a up to but NOT including index b. The length of the result from substring(a,b) is b-a.
== checks if two variables reference the same object in memory. Two different String objects can have identical content but be stored at different memory addresses, making == return false even when the content is the same. .equals() correctly compares content.
compareTo() returns a negative integer if the calling String comes before the argument alphabetically, 0 if they are equal, and a positive integer if the calling String comes after. The exact magnitude doesn't matter — only the sign.
Strings are immutable in Java. Once created, the characters in a String cannot be changed. Methods like toUpperCase(), substring(), and toLowerCase() always return a new String. To use the result, you must assign it back: s = s.toUpperCase().
Tanner Crow — AP CS Teacher & Tutor
11+ years teaching AP Computer Science at Blue Valley North High School (Overland Park, KS). Verified Wyzant tutor with 1,845+ hours, 451+ five-star reviews, and a 5.0 rating. His AP CSA students score 5s at more than double the national rate.
- 54.5% of students score 5 on AP CSA (national avg: 25.5%)
- 1,845+ verified tutoring hours • 5.0 rating
- Free 1-on-1 tutoring inquiry: Wyzant Profile
🔗 Related Topics
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]