ACSA Exam Prep
Unit 1 · Using Objects and Methods · Topic 1.15

String Manipulation

Skill 2.CSkill 3.CSkill 4.ASkill 4.B

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.

Learning objectives
  • 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.

Index01234567
Charcomputer

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).

Code
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());
Output
8
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.

Code
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);
Output
true
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.

Code
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]);
Output
data
ata
3 green

Pattern: walk a string one character at a time

Code
String 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);
Output
3
Key points
  • Strings are immutable. Methods return new strings.
  • + and += concatenate; primitives are converted to text; objects use toString.
  • Indexes run 0 to length() - 1; outside that throws StringIndexOutOfBoundsException.
  • Quick Reference: length, substring(from, to), substring(from), indexOf, equals, compareTo, split.
  • One character: s.substring(i, i + 1).
Watch out
  • Compare contents with equals, not ==.
  • s.substring(2); by itself changes nothing.
  • substring(a, b) has length b - a, and b may equal length().
  • s.substring(s.length()) is legal and returns ""; s.substring(s.length() + 1) throws.
Exam tip
  • Write the indexes under the letters before tracing any substring question.
Quick check
What is printed?
String s = "programming";
System.out.println(s.substring(s.indexOf("g"), s.indexOf("g") + 3));
Show answer
"gra". The first g is at index 3, so substring(3, 6).
What does "Mississippi".indexOf("ss") return?
Show answer
2.
Not on the exam
  • Overriding toString.
  • Using equals to compare a String with a non-String object.