1.15 String manipulationStrings: text you can slice
A String is an object that holds a sequence of characters. String is a class in java.lang, but it gets special treatment: a String literal like "rain" creates a String object for you, without new. (You can also write new String("rain"), and the Quick Reference lists that constructor.)
Strings are immutable: once a String object is made, its characters can never change. Every String method that seems to change a String actually builds and returns a new one. So s.substring(1) leaves s exactly as it was. If you want s to change, you have to assign the result back: s = s.substring(1);.
?Wait, how did + work with Strings again?
Indexes
Each character in a String has a position number called an index, and indexes start at 0. In "basketball", b is at index 0 and the last l is at index 9. The length is 10, so the last index is always length() - 1. Using an index outside 0 to length() - 1 throws a StringIndexOutOfBoundsException, a kind of IndexOutOfBoundsException.
| index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|---|
| s | b | a | s | k | e | t | b | a | l | l |
| substring(3, 6) | k | e | t | |||||||
| substring(6) | b | a | l | l |
?Wait, why is the last index length() - 1 and not length()?
The methods on the Quick Reference
length() returns the number of characters. substring(from, to) returns the characters from index from up to, but not including, index to, so its length is to - from. substring(from) returns everything from index from to the end. To get the single character at index k, as a String, use substring(k, k + 1). indexOf(str) returns the index where str first appears, or -1 if it doesn't appear at all.
substring is forgiving at exactly one spot: an index equal to length() is allowed, as long as you don't go past it. "basketball".substring(10) is the empty String "", and substring(0, 10) is the whole word. substring(11), or a from bigger than to, throws an exception.
1String s = "basketball";2System.out.println(s.length());13System.out.println(s.substring(6));24System.out.println(s.substring(3, 6));25System.out.println(s.indexOf("ball"));36System.out.println(s.indexOf("a"));37System.out.println(s.indexOf("Ball"));48System.out.println(s.substring(s.length() - 1));5
- line 2 10 characters, so indexes 0 to 9
- line 3-4 Start index included, end index not
- line 5-6 Where the first match starts
- line 7 Not found (B is not b): -1
- line 8 The last character, as a String
I thought about it. Show me
Comparing Strings
equals(other) returns true when two Strings contain exactly the same characters in the same order. It's case-sensitive, so "maple".equals("Maple") is false. compareTo(other) compares alphabetically, character by character, and returns an int: negative if this String comes first, positive if it comes after, and 0 if they're equal. Only the sign matters on the exam.
compareTo looks at the first position where the two Strings differ. If one String is a prefix of the other, the shorter one comes first: "man" comes before "mango". And it compares character codes, where every capital letter comes before every lowercase letter, so "Zebra" comes before "apple". Never use == to compare String contents. It checks whether two references point to the same object, which is a different question (more on that in Unit 2).
1String a = "maple";2String b = "mango";3System.out.println(a.equals("Maple"));14System.out.println(a.compareTo(b) > 0);25System.out.println(b.compareTo("man") > 0);36System.out.println("Zebra".compareTo("apple") < 0);47System.out.println(a.compareTo("maple"));5
- line 3 equals is case-sensitive
- line 4 First difference: p comes after n
- line 5 A prefix comes first, so mango is after man
- line 6 Every capital letter comes before every lowercase one
- line 7 Same characters: exactly 0
- Find the comma: int comma = full.indexOf(",");. For "Lopez, Maria", comma is 5.
Why?
Never hard-code 5. The question says "any name," and indexOf finds the position for each one. - The last name is everything before the comma: String last = full.substring(0, comma);. That's indexes 0 to 4: "Lopez".
Why?
substring stops before the end index, which is exactly what you want: the comma itself is left out. - The first name starts two characters after the comma (skip the comma and the space): String first = full.substring(comma + 2);. That's "Maria".
- Glue them with a space: String flipped = first + " " + last;. That's "Maria Lopez".
Answer:
int comma = full.indexOf(",");
String last = full.substring(0, comma);
String first = full.substring(comma + 2);
String flipped = first + " " + last;Stuck? Get a hint
- Which method tells you where the comma is?
- The last name ends right before the comma. substring's end index is not included.
- The first name starts after the comma and the space: comma + 2.
String w = "racecar";
String t = w.substring(w.indexOf("e"));
System.out.println(t + w.indexOf("car"));- Aecar4
- Becar3
- Ccecar4
- Dcar4
Show the answer
A. r0 a1 c2 e3 c4 a5 r6. indexOf("e") is 3, so substring(3) is "ecar". indexOf("car") finds the first "car", which starts at 4. A String plus an int concatenates: "ecar4". Choice B mixes up the two searches: 3 is where "e" is, but "car" starts at 4.
- A"apple".compareTo("Apple") < 0
- B"cat".compareTo("catalog") < 0
- C"dog".equals("Dog")
- D"pear".compareTo("peach") < 0
Show the answer
B. "cat" is a prefix of "catalog", so it comes first and compareTo is negative. In A, lowercase a comes after capital A, so the result is positive. C is false because equals is case-sensitive. In D, the first difference is r versus c, and r comes after c, so the result is positive.

Before you touch any String question, write the word and number its letters from 0. Then for every substring call, write the start and the end and say out loud "up to, not including." It takes ten seconds and it is the difference between the right answer and the trap.
- Strings are immutable. Methods return new Strings; assign the result if you want to keep it.
- Indexes run from 0 to length() - 1. Going outside throws StringIndexOutOfBoundsException.
- substring(a, b): from a up to, not including, b (length b - a). substring(a): from a to the end. substring(k, k + 1): one character.
- indexOf returns the first match's index, or -1.
- equals compares characters exactly (case counts). compareTo: negative, 0 or positive. Never == for contents.