String Manipulation
Big idea: Strings are indexed from 0, and no String method ever changes the original. Master substring and indexOf and most String questions become easy.
- Develop code to create string objects and determine the result of creating and combining strings.
- Develop code to call methods on string objects and determine the result of calling these methods.
Indexes
Each character has an index starting at 0. The last index is length() - 1. Using an index outside that range throws a StringIndexOutOfBoundsException.
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|---|
| Char | c | o | m | p | u | t | e | r |
The Quick Reference methods
substring(from, to) includes from and stops before to, so its length is to - from. substring(from) goes to the end. indexOf(str) returns the first match's index, or −1. One character is s.substring(i, i + 1).
String s = "computer";
System.out.println(s.length());
System.out.println(s.substring(3, 6));
System.out.println(s.substring(5));
System.out.println(s.substring(0, 1));
System.out.println(s.indexOf("ute"));
System.out.println(s.indexOf("z"));
System.out.println(s.substring(2, 2).length());Output8 put ter c 4 -1 0
Comparing strings
equals compares contents. == only checks whether two references point to the same object, so never use it for content. compareTo returns a negative number if this string comes first alphabetically, 0 if equal, and positive if it comes after. Uppercase letters come before lowercase.
String a = "apple";
String b = new String("apple");
System.out.println(a.equals(b));
System.out.println(a == b);
System.out.println("apple".compareTo("banana") < 0);
System.out.println("Zoo".compareTo("ant") < 0);Outputtrue false true true
Immutability and split
Strings are immutable: methods return new strings. To keep a result, assign it. split(delim) breaks a string into an array of pieces.
String s = "data";
s.substring(1);
System.out.println(s);
s = s.substring(1);
System.out.println(s);
String[] parts = "red,green,blue".split(",");
System.out.println(parts.length + " " + parts[1]);Outputdata ata 3 green
Pattern: walk a string one character at a time
CodeString word = "banana";
int count = 0;
for (int i = 0; i < word.length(); i++) {
if (word.substring(i, i + 1).equals("a")) {
count++;
}
}
System.out.println(count);Output3- Strings are immutable. Methods return new strings.
+and+=concatenate; primitives are converted to text; objects usetoString.- Indexes run 0 to
length() - 1; outside that throwsStringIndexOutOfBoundsException. - Quick Reference:
length,substring(from, to),substring(from),indexOf,equals,compareTo,split. - One character:
s.substring(i, i + 1).
- Compare contents with
equals, not==. s.substring(2);by itself changes nothing.substring(a, b)has lengthb - a, andbmay equallength().s.substring(s.length())is legal and returns"";s.substring(s.length() + 1)throws.
- Write the indexes under the letters before tracing any substring question.
String s = "programming";
System.out.println(s.substring(s.indexOf("g"), s.indexOf("g") + 3));Show answer
Show answer
- Overriding toString.
- Using equals to compare a String with a non-String object.