2.4

String Methods

AP Computer Science A

Strings are objects — and they're immutable

java
String name = "Alice";
name.toUpperCase();            // returns "ALICE" but name is still "Alice"!
name = name.toUpperCase();     // NOW name is "ALICE" (reassigned)

String indexing

String:  "H  e  l  l  o"
Index:    0  1  2  3  4

Essential String methods

length()

java
"Hello".length()     // 5
"".length()          // 0 (empty string)
" Hi ".length()      // 4 (spaces count!)

substring(int from, int to)

java
String s = "Hello, World!";
//          0123456789...

s.substring(0, 5)     // "Hello"
s.substring(7, 12)    // "World"
s.substring(0, 1)     // "H" (just the first character)

substring(int from)

java
String s = "Hello, World!";

s.substring(7)        // "World!"
s.substring(0)        // "Hello, World!" (entire string)
s.substring(12)       // "!" (just the last character)

indexOf(String str)

java
String s = "Hello, World!";

s.indexOf("World")    // 7
s.indexOf("o")        // 4 (first occurrence)
s.indexOf("xyz")      // -1 (not found)
s.indexOf("Hello")    // 0

equals(String other)

java
"Hello".equals("Hello")    // true
"Hello".equals("hello")    // false (case matters!)
"Hello".equals("Hi")       // false

compareTo(String other)

java
"apple".compareTo("banana")   // negative (apple comes first)
"banana".compareTo("apple")   // positive (banana comes after)
"cat".compareTo("cat")        // 0 (equal)

Combining methods — real exam patterns

Get the first character

java
String name = "Alice";
String first = name.substring(0, 1);   // "A"

Get the last character

java
String name = "Alice";
String last = name.substring(name.length() - 1);   // "e"

Check if a string starts with something

java
String word = "preview";
if (word.substring(0, 3).equals("pre")) {
    System.out.println("Starts with pre!");
}

Reverse traversal of characters

java
String word = "Java";
String reversed = "";
for (int i = word.length() - 1; i >= 0; i--) {
    reversed += word.substring(i, i + 1);
}
// reversed = "avaJ"

Extract and build

java
String email = "alice@example.com";
int atPos = email.indexOf("@");
String username = email.substring(0, atPos);       // "alice"
String domain = email.substring(atPos + 1);         // "example.com"

Trace practice — the full workout

java
String s = "AP Computer Science A";
int len = s.length();
int space1 = s.indexOf(" ");
String first = s.substring(0, space1);
String rest = s.substring(space1 + 1);
int space2 = rest.indexOf(" ");
String second = rest.substring(0, space2);
String last = s.substring(s.length() - 1);

String concatenation with +

java
String first = "Hello";
String last = "World";
String full = first + " " + last;   // "Hello World"

// Numbers are converted to strings automatically:
String msg = "Score: " + 95;         // "Score: 95"
String calc = 3 + 4 + " items";     // "7 items" (3+4 first, then concat)
String calc2 = "items: " + 3 + 4;   // "items: 34" (concat left to right!)

AP Exam Tips

Common Mistakes

Key Vocabulary