01 / 20
S t r 7

String
Manipulation

Master the essential String methods in Java: extracting, comparing, and transforming text for the AP Computer Science A exam.

AP Computer Science A

What Is a String?

In Java, a String is an immutable sequence of characters. Once created, its contents cannot be changed: every method that seems to modify a String actually returns a new String object.

'H' 0 'e' 1 'l' 2 'l' 3 'o' 4 length() = 5 "Hello"

Immutability: Strings Never Change

When you call a method like toUpperCase() or substring(), the original String stays the same. A brand-new String is returned. If you don't store the result, it is lost.

String s = "hello"; s.toUpperCase(); // returns "HELLO" but s is still "hello" s = s.toUpperCase(); // NOW s is "HELLO"

Zero-Based Indexing

Each character in a String has an index starting at 0. The last character is at index \( \text{length()} - 1 \). Accessing an index outside this range throws a StringIndexOutOfBoundsException.

"Java!" 'J' 0 'a' 1 'v' 2 'a' 3 '!' 4 length() = 5 last index = 4

Core String Methods

length()
Returns the number of characters in the String.
charAt(i)
Returns the character at index i.
substring(a, b)
Returns characters from index a up to but not including b.
indexOf(str)
Returns the first index where str is found, or -1 if absent.
equals(other)
Returns true if content matches. Never use == for String comparison.
compareTo(other)
Returns negative, zero, or positive based on lexicographic order.

The substring() Method

substring(beginIndex, endIndex) extracts characters starting at beginIndex up to but not including endIndex. There is also a one-argument form: substring(beginIndex) goes from that index to the end.

"Coding" 'C' 0 'o' 1 'd' 2 'i' 3 'n' 4 'g' 5 substring(1, 4) → "odi"

equals() vs ==

equals()
Compares the content of two Strings character by character. This is what you almost always want.
== operator
Compares memory references: whether two variables point to the exact same object. Unreliable for String content.
String a = new String("hi"); String b = new String("hi"); a == b // false (different objects) a.equals(b) // true (same content)

Lexicographic Order: compareTo()

str1.compareTo(str2) returns an integer based on lexicographic (dictionary) order. It compares character by character using Unicode values.

Negative
str1 comes before str2
Zero
Strings are equal
Positive
str1 comes after str2
"apple".compareTo("banana") // negative (a < b) "cat".compareTo("cat") // 0 (equal) "dog".compareTo("cat") // positive (d > c)

How to Apply String Methods

1
Identify the task. Are you extracting, searching, comparing, or transforming?
2
Pick the right method. Use substring() for extraction, indexOf() for searching, equals() for comparison.
3
Calculate indices carefully. Remember: zero-based, and endIndex is exclusive.
4
Store the result. Strings are immutable: capture the return value in a variable.
5
Trace your code. Walk through each character and index to verify your logic.

Example 1: Extracting a Substring

Problem: Given "Computer", extract the word "put".

String word = "Computer"; String result = word.substring(3, 6); // result is "put"
'C' 0 'o' 1 'm' 2 'p' 3 'u' 4 't' 5 'e' 6 'r' 7 substring(3, 6) → "put"

Example 2: Searching with indexOf()

Problem: Find the position of "Science" in the String "Computer Science A", then extract it.

String course = "Computer Science A"; int pos = course.indexOf("Science"); // pos = 9 String sub = course.substring(pos, pos + 7); // sub is "Science"

Example 3: Traversing a String

Problem: Count how many times the letter 'a' appears in "banana".

String word = "banana"; int count = 0; for (int i = 0; i < word.length(); i++) { if (word.charAt(i) == 'a') { count++; } } // count is 3

Concatenation with +

The + operator joins Strings together. When you concatenate a non-String value (like an int), Java automatically converts it to a String first.

String name = "Score"; int val = 95; String msg = name + ": " + val; // msg is "Score: 95" String tricky = 3 + 4 + "hi"; // tricky is "7hi" (int addition first, then concat) String tricky2 = "hi" + 3 + 4; // tricky2 is "hi34" (concat from left to right)

Common Mistakes

✕ Using == for Strings
Comparing String content with == checks references, not values. Use .equals() instead.
✕ Off-by-One in substring()
Forgetting that endIndex is exclusive. substring(2, 5) gives 3 characters, not 4.
✕ Ignoring Immutability
Calling s.toUpperCase() without storing the result: s stays unchanged.
✕ Index Out of Bounds
Accessing charAt(length()) instead of charAt(length() - 1) for the last character.
✕ Concat Order Confusion
Expecting 2 + 3 + "x" to produce "23x" when it actually produces "5x".

Method Quick Reference

Method Returns Example
length() int "Hello".length() → 5
charAt(i) char "Hello".charAt(1) → 'e'
substring(a, b) String "Hello".substring(1,4) → "ell"
indexOf(str) int "Hello".indexOf("ll") → 2
equals(other) boolean "Hi".equals("Hi") → true
compareTo(other) int "a".compareTo("b") → negative

Question 1 of 5

What is the output of the following code?

String s = "AP Exam";
System.out.println(s.substring(3, 7));

Question 2 of 5

What does the following code print?

String s = "hello";
s.toUpperCase();
System.out.println(s);

Question 3 of 5

What is the value of result after this code executes?

String s = "Java";
int result = s.indexOf("va");

Question 4 of 5

What does the following expression evaluate to?

2 + 3 + "CS" + 4 + 5

Question 5 of 5

Which code correctly checks if String s ends with the character 'e'?