Topic 1.15 Notes – String Manipulation
1. What a String Is
A String object represents a sequence of characters, like "hello" or "APCSA".
You can create a String in two ways:
String name = "Jordan"; // string literal (most common)
String other = new String("Jordan"); // constructor (rarely needed)
Both create a String object. The class String is in java.lang, which Java automatically imports for you.
An empty String is valid:
String s = ""; // length is 0
Immutability
This is the core concept.
Strings are immutable. Once created, their characters cannot change.
When you call a method on a String, you get a new String back.
String word = "code";
word.toUpperCase(); // does nothing useful
System.out.println(word); // still "code"
word = word.toUpperCase();
System.out.println(word); // now "CODE"
Think of String methods as producers, not editors. If you don’t assign the result, the original stays the same. This shows up constantly in multiple-choice tracing questions.
2. Concatenation and Combining Strings
Using + and +=
The + operator joins Strings together.
String full = "AP" + " " + "Exam"; // "AP Exam"
The += operator appends and reassigns:
String message = "Score:";
message += " 88"; // creates a new String
Each concatenation creates a new object because of immutability.
Concatenating with primitives
Primitives automatically convert to Strings during concatenation.
"Level: " + 3 // "Level: 3"
Order matters:
"2 + 4 = " + 2 + 4 // "2 + 4 = 24"
Java evaluates left to right. Once it sees a String, everything after becomes String concatenation.
Concatenating with objects
When you concatenate an object:
Object obj = new Object();
String s = "Object: " + obj;
Java automatically calls obj.toString().
Every class inherits toString() from Object. You do not need to override toString() for the AP exam, but you should understand that it’s being called.
3. String Indexing and Valid Ranges
Strings use 0-based indexing, just like arrays.
If:
String text = "HELLO";
Valid indices go from:
0 to text.length() - 1
For "HELLO":
length()is 5- Valid indices are
0-4
Accessing outside this range causes a StringIndexOutOfBoundsException at runtime.
Single-character substring trick
To get one character as a String:
text.substring(i, i + 1);
The ending index is exclusive, so this grabs exactly one character.
4. Required String Methods You Must Know
These are straight from the Java Quick Reference.
int length()
Returns number of characters.
"AP".length(); // 2
String substring(int from, int to)
Returns characters from index from up to to - 1.
Number of characters returned = to - from.
String s = "computer";
s.substring(2, 5); // "mpu"
Indices 2, 3, 4 are included. Index 5 is excluded.
Common mistake is thinking the end index is included. It is not.
String substring(int from)
Returns from from to the end.
s.substring(3); // same as s.substring(3, s.length())
int indexOf(String str)
Returns the starting index of the first occurrence.
Returns -1 if not found.
String s = "banana";
s.indexOf("na"); // 2
s.indexOf("z"); // -1
If you see code like:
if (s.indexOf("x") != -1)
That’s checking whether "x" exists.
Forgetting about -1 is a classic exam trap.
boolean equals(Object other)
Compares contents, not memory locations.
String a = "cat";
String b = "cat";
a.equals(b); // true
Do not use == to compare String contents. == checks if they are the same object.
int compareTo(String other)
Alphabetical comparison (lexicographic order).
< 0→ this comes before other0→ equal> 0→ this comes after other
"apple".compareTo("banana"); // negative
Only the sign matters, not the exact number.
5. How This Appears on Tests
You’ll often see combinations like:
substring+indexOf- concatenation inside loops
- checking
equals - forgetting to store returned values
A very common FRQ pattern is extracting parts of a formatted string:
String email = "user123@domain.com";
int at = email.indexOf("@");
String username = email.substring(0, at);
This mixes:
- indexing
indexOfsubstring- understanding of exclusive bounds