01
Overview

String methods: the AP CSA toolkit for text.

A short list of well-defined methods unlocks nearly every String problem on the AP exam.

A String in Java is an object representing a sequence of characters. The String class is immutable — once created, its contents never change. Any method that "modifies" a String actually returns a new String.

The AP CSA exam tests a focused subset of String methods. You don't need to memorize the entire String API — just the ones in the official AP CSA Java Quick Reference.

Mastering these few methods is the single highest-leverage skill in Unit 2: they appear in MCQs, in FRQs, and as building blocks for nearly every text-processing problem.

02
Core Concept

The AP CSA String methods you must know.

Each one returns a new value; the original String is never changed.

length()

String s = "Hello";
int n = s.length();   // 5

Returns the number of characters in the String.

substring(int from, int to)

String s = "Computer";
String mid = s.substring(2, 5);   // "mpu"

Returns the substring from index from (inclusive) to to (exclusive). The length of the result is to - from.

substring(int from)

String tail = s.substring(3);   // "puter"

Returns the substring from from to the end.

indexOf(String str)

int i = "Hello".indexOf("ll");   // 2
int j = "Hello".indexOf("z");    // -1

Returns the index of the first occurrence of str, or -1 if not found.

equals(Object other)

"cat".equals("cat");   // true
"cat".equals("Cat");   // false (case-sensitive)

Compares String contents, not references. Never use == for String comparison.

compareTo(String other)

"apple".compareTo("banana");   // negative
"apple".compareTo("apple");    // 0
"banana".compareTo("apple");   // positive

Returns a negative number, zero, or a positive number depending on dictionary order. Only the sign matters for AP reasoning.

Concatenation with +

String greet = "Hello, " + "world";   // "Hello, world"
String s = "Score: " + 95;             // "Score: 95"

When either operand of + is a String, Java performs concatenation and converts the other operand to a String.

03
Key Vocabulary

String terminology used in every AP question.

Tight vocabulary makes MCQ reading much faster.

Vocabulary
  • Immutable — cannot be changed after creation.
  • Substring — a contiguous slice of a String.
  • Index — zero-based position of a character.
  • Concatenation — joining Strings with +.
  • Lexicographic order — dictionary-style comparison used by compareTo.
  • String literal — a value written in quotes like "hello".
  • indexOf — returns position or -1 when not found.
04
AP Exam Focus

How String methods are tested.

Index arithmetic, immutability, and == vs .equals() dominate.

Exam Focus

MCQ patterns to expect:

  • Substring math. s.substring(a, b) returns characters at indices a through b - 1. The length is b - a.
  • Immutability traps. A line like s.toUpperCase(); alone changes nothing because the result isn't stored.
  • == vs .equals. Always use .equals() for content comparison.
  • indexOf returning -1. If the substring isn't there, the result is -1 — handle it before using as an index.
  • Out-of-bounds substring. s.substring(0, s.length() + 1) throws StringIndexOutOfBoundsException.

FRQ tip: when extracting text, always use s.length() rather than hard-coded numbers.

05
Worked Example

Use String methods to extract a domain.

Combine indexOf, substring, and length checks.

Worked Example AP-style reasoning

Problem. Given String email = "ana@school.edu";, return the part after the @.

String email = "ana@school.edu";
int at = email.indexOf("@");
String domain = email.substring(at + 1);
System.out.println(domain);

Step 1. email.indexOf("@") finds the index of "@", which is 3.

Step 2. email.substring(at + 1) returns the characters starting at index 4 through the end, which is "school.edu".

Step 3. Print "school.edu".

Robust version. If "@" might not appear, indexOf returns -1. Always guard:

if (at != -1) {
    String domain = email.substring(at + 1);
}

Why this matters. The AP exam expects you to know that indexOf returns -1 on failure and to use it carefully with substring.

06
Common Mistakes

String errors students repeat year after year.

All of these are easy to avoid once you see the pattern.

Watch Out
  • Using == for Strings. Compares references, not contents. Use .equals().
  • Treating String methods as mutators. s.toUpperCase(); alone does nothing; assign the result.
  • Off-by-one in substring. The second argument is exclusive: s.substring(0, 3) returns three characters, indices 0–2.
  • Out-of-bounds index. s.charAt(s.length()) or s.substring(0, s.length() + 1) both throw exceptions.
  • Ignoring -1 from indexOf. Passing -1 straight into substring will crash.
  • Wrong concatenation type. "" + (a + b) may behave differently than "" + a + b for numbers — parentheses matter.
07
Reference Table

AP CSA String method quick reference.

Exactly the methods the College Board provides on the exam.

String Methods Reference

AP Quick Reference
Method Returns AP Exam Tip
length() Number of characters (int) Use as the loop bound for traversal.
substring(from, to) Characters from index from to to - 1 Length of result is to - from.
substring(from) Characters from from to end Use after indexOf for "rest of String".
indexOf(str) First index of str, or -1 Always check for -1 before using the result.
equals(other) true or false Use instead of == for content equality.
compareTo(other) Negative / zero / positive (int) Only the sign matters in reasoning.
+ (concatenation) New String combining operands Any operand becomes a String when one side is a String.
08
Practice Tip

How to lock in the AP String toolkit.

A handful of methods, used precisely, solves nearly every problem.

Practice Strategy

Use the three practice tools below this lesson in order:

  • MCQ Practice — for every substring question, write the index of each character of the String before answering. This eliminates off-by-one errors.
  • Java Lab — practise the classic patterns: extract a domain, split a name, reverse a String using substring, count vowels. Always store the return value.
  • FRQ Practice — combine indexOf, substring, and equals to process text. Comment your index math so graders can follow your logic.

If you can write each AP String method's signature from memory, you're ready for any text problem on the exam.

09
Section · 09

Practice — attempt these now.

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

Topic Quiz 30 min 24 marks Pending

AP CSA Topic Practice: String Methods

AP-style topic practice assessment

Start
FRQ Practice 30 min 18 marks Pending

AP CSA Unit 01 Topic 05 String Methods FRQ Practice

AP-style topic practice assessment

Start