Which String methods does AP CSA require, and how do length, substring, indexOf, equals and compareTo behave?
Topic 2.7 String Methods: call the String methods in the AP Java subset (length, substring, indexOf, equals, compareTo), respecting zero-based indexing and the immutability of String objects.
A focused answer to AP CSA Topic 2.7, covering the required String methods length, substring (both forms), indexOf, equals and compareTo, zero-based indexing, the half-open range of substring, why == differs from equals, and String immutability, with a worked trace.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this topic is asking
The College Board (Topic 2.7) wants you to call the String methods in the AP Java subset: length, substring (two forms), indexOf, equals and compareTo. You must use zero-based indexing, respect the half-open range of substring, know why equals (not ==) compares contents, and remember that Strings are immutable so these methods return new values.
Zero-based indexing
For "CAT": C is index 0, A is index 1, T is index 2, and length() is 3.
The required methods
length()returns the number of characters as anint."hello".length()is5.substring(from)returns the substring from indexfromto the end."hello".substring(2)is"llo".substring(from, to)returns the characters from indexfromup to but not includingto(a half-open range[from, to)). The length of the result isto - from."hello".substring(1, 4)is"ell".indexOf(str)returns the index of the first occurrence ofstr, or-1ifstrdoes not appear."banana".indexOf("na")is2;"banana".indexOf("z")is-1.equals(other)returnstrueif this String has the same characters asother, elsefalse.compareTo(other)returns a negative number if this String comes beforeotherin dictionary (lexicographic) order,0if they are equal, and a positive number if it comes after.
equals versus ==
This is one of the most tested traps in Unit 2: always compare Strings with equals (or compareTo), never with ==.
Immutability
Because Strings are immutable, none of these methods changes the original String. substring, for instance, returns a new String; the original is untouched. So you must capture the result: word = word.substring(1); reassigns word, whereas word.substring(1); on its own throws the new String away.
Try this
Q1. State the value of "COMPUTER".substring(2, 5). [1 point]
- Cue.
"MPU"- indices 2, 3, 4 (up to but not including 5).
Q2. Explain why you should use equals rather than == to test whether two String variables hold the same text. [2 points]
- Cue.
==compares references (whether they are the same object), so two Strings with identical contents can givefalse;equalscompares the actual characters, which is what you want.
Exam-style practice questions
Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AP 2021 (style)1 marksMultiple choice. Consider the following code segment.
```java
String s = "COMPUTER";
System.out.println(s.substring(3, 6));
```
What is printed?
(A) `PUT`
(B) `PUTE`
(C) `MPU`
(D) `MPUT`
(E) `UTE`
Show worked answer →
The answer is (A).
Indexing is zero-based, so C=0, O=1, M=2, P=3, U=4, T=5, E=6, R=7. substring(3, 6) returns the characters from index 3 up to but not including index 6, that is indices 3, 4 and 5: P, U, T, giving "PUT". (B) wrongly includes index 6; (C) and (D) start at the wrong index; (E) misreads the range.
Markers reward zero-based indexing and the half-open [start, end) range of the two-argument substring.
AP 2020 (style)4 marksFree response (code writing). A String `word` of length at least 2 is given. Write a code segment that prints the first character of `word`, the last character of `word`, and `true` or `false` for whether `word` equals the String `"STOP"`, each on its own line. Use String methods only (no loops).
Show worked answer →
A 4-point question testing substring, length and equals together.
System.out.println(word.substring(0, 1));
System.out.println(word.substring(word.length() - 1));
System.out.println(word.equals("STOP"));
Point 1: word.substring(0, 1) returns the first character (index 0 up to, not including, 1). Point 2: word.substring(word.length() - 1) returns from the last index to the end - the last character. Point 3: word.equals("STOP") compares contents and returns a boolean. Point 4: printing each on its own line. Using == instead of equals would compare references, not contents, and is the classic error here.
Related dot points
- Topic 2.6 String Objects: Concatenation, Literals, and More: create String objects from literals or a constructor, concatenate strings with the + operator, and predict the result of mixing strings with numbers and escape sequences.
A focused answer to AP CSA Topic 2.6, covering String literals and the String constructor, concatenation with the plus operator, the rule that any value concatenated with a String becomes a String, left-to-right evaluation traps, and escape sequences, with a worked trace.
- Topic 2.5 Calling a Non-void Method: call a method that returns a value, and use the returned value by storing it, printing it, or including it in an expression.
A focused answer to AP CSA Topic 2.5, covering methods that return a value, the return type, using a returned value in an assignment, expression or print, the difference from a void method, and chaining method calls, with a worked trace.
- Topic 2.8 Wrapper Classes: Integer and Double: use the Integer and Double wrapper classes, including autoboxing and unboxing, the MIN_VALUE and MAX_VALUE constants, and parsing methods.
A focused answer to AP CSA Topic 2.8, covering why wrapper classes exist, creating Integer and Double objects, autoboxing and unboxing, Integer.MIN_VALUE and MAX_VALUE, parseInt and parseDouble, and the == versus equals trap for wrappers, with a worked trace.
- Topic 2.9 Using the Math Class: call the static Math methods in the AP Java subset (abs, pow, sqrt, random) and generate a random integer or double in a specified range.
A focused answer to AP CSA Topic 2.9, covering the required static Math methods abs, pow, sqrt and random, why Math methods are called on the class, the return types, and the standard formula for generating a random int in a range, with a worked trace.
- Topic 2.4 Calling a Void Method with Parameters: call a void method that takes parameters, passing arguments that match the parameter list in number, order and type, and explain how arguments are copied to parameters.
A focused answer to AP CSA Topic 2.4, covering parameters versus arguments, matching the argument list in number, order and type, pass-by-value for primitives, overloaded methods, and how the call supplies data to the method, with a worked trace.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)