Skip to main content
United StatesComputer ScienceSyllabus dot point

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.

Generated by Claude Opus 4.811 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. Zero-based indexing
  3. The required methods
  4. equals versus ==
  5. Immutability
  6. Try this

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 an int. "hello".length() is 5.
  • substring(from) returns the substring from index from to the end. "hello".substring(2) is "llo".
  • substring(from, to) returns the characters from index from up to but not including to (a half-open range [from, to)). The length of the result is to - from. "hello".substring(1, 4) is "ell".
  • indexOf(str) returns the index of the first occurrence of str, or -1 if str does not appear. "banana".indexOf("na") is 2; "banana".indexOf("z") is -1.
  • equals(other) returns true if this String has the same characters as other, else false.
  • compareTo(other) returns a negative number if this String comes before other in dictionary (lexicographic) order, 0 if 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 give false; equals compares 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

Sources & how we know this