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.

💻 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:

Example 1: length, charAt, substring, indexOf
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
    }
}
Running…

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:

Example 2: equals vs ==, compareTo
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)
    }
}
Running…

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:

Example 3: Case Methods and Immutability
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
    }
}
Running…

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.

1
⚠ Using == instead of .equals() to compare Strings

== 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
2
⚠ Forgetting substring end index is exclusive

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
3
⚠ Expecting String methods to modify the original

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
4
⚠ Misreading compareTo() return value

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)
🎓 AP Exam Tip

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.

⚠ Watch Out!

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)

Your Score: 0 / 0
1. What does "computer".substring(3, 6) return?
2. What does "hello".indexOf("l") return?
3. What is the correct way to check if two Strings have the same content?
4. What is printed?
String s = "AP CSA";
s.toLowerCase();
System.out.println(s);
5. What does "banana".compareTo("apple") return?
6. What is the length of "hello".substring(1, 4)?
7. What does "search".indexOf("x") return?
8. Which statement is TRUE about Java Strings?

❓ Frequently Asked Questions

What String methods do I need to know for AP CSA?

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.

What is the difference between substring(a) and substring(a,b)?

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.

Why should I use .equals() instead of == for Strings?

== 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.

What does compareTo() return?

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.

Are Java Strings mutable or immutable?

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().

TC

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

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]