Unit 1.15: String Manipulation

AP Computer Science A

Learning Objectives

1.15.A: Develop code to create string objects and determine the result of creating and combining strings.

1.15.B: Develop code to call methods on string objects and determine the result of calling these methods.

What is a String?

A String object represents a sequence of characters.

Creating Strings

String greeting = "Hello"; String name = new String("World");

Key Facts

  • The String class is part of java.lang package
  • Classes in java.lang are available by default (no import needed)
  • Strings are immutable - once created, they cannot be changed

String Immutability

Strings are immutable - methods called on a String do not change the original String object.

String original = "Hello"; String modified = original.toUpperCase(); System.out.println(original); // "Hello" (unchanged) System.out.println(modified); // "HELLO" (new String)

Important: String methods return NEW String objects rather than modifying the original.

String Concatenation

Strings can be combined using the + or += operator.

String first = "Hello"; String last = "World"; String message = first + " " + last; // "Hello World" String greeting = "Count: "; greeting += 5; // "Count: 5" (int converted to String)

Key Points

  • Concatenation creates a new String object
  • Primitive values are implicitly converted to Strings
  • Any object can be concatenated (calls toString() method)

String Indexing

Strings use zero-based indexing: indices range from 0 to length() - 1

String word = "JAVA"; // J A V A // Index: 0 1 2 3 // Length: 4

Warning: Accessing an index outside the range [0, length()-1] results in a StringIndexOutOfBoundsException

String word = "JAVA"; word.substring(0, 10); // Exception! Index 10 is out of bounds

String Method: length()

int length()

Returns the number of characters in the String.

String message = "Hello"; int len = message.length(); // 5 String empty = ""; int emptyLen = empty.length(); // 0

Common Use

Often used in loops to process each character in a String:

for (int i = 0; i < message.length(); i++) { // Process each character }

String Method: substring()

String substring(int from, int to)

Returns the substring from index from to index to - 1

String substring(int from)

Returns substring from index from to the end

String text = "Computer"; // 01234567 String sub1 = text.substring(0, 3); // "Com" String sub2 = text.substring(3); // "puter" String sub3 = text.substring(4, 7); // "ute" // Single character at index i: String ch = text.substring(i, i+1);

String Method: indexOf()

int indexOf(String str)

Returns the index of the first occurrence of str. Returns -1 if not found.

String sentence = "Hello World Hello"; int index1 = sentence.indexOf("World"); // 6 int index2 = sentence.indexOf("Hello"); // 0 (first occurrence) int index3 = sentence.indexOf("Java"); // -1 (not found) int index4 = sentence.indexOf("o"); // 4

Tip: Always check if the return value is -1 before using it as an index!

String Method: equals()

boolean equals(Object other)

Returns true if this String has the same sequence of characters as other.

String s1 = "Hello"; String s2 = "Hello"; String s3 = "hello"; s1.equals(s2); // true s1.equals(s3); // false (case-sensitive!) s1 == s2; // DON'T USE == for Strings!

Important: Always use equals() to compare String content, not ==

The == operator compares memory references, not content.

String Method: compareTo()

int compareTo(String other)

Compares two Strings lexicographically (alphabetically).

  • Returns negative value if this < other
  • Returns 0 if this equals other
  • Returns positive value if this > other
String a = "Apple"; String b = "Banana"; String c = "Apple"; a.compareTo(b); // negative (Apple < Banana) b.compareTo(a); // positive (Banana > Apple) a.compareTo(c); // 0 (Apple = Apple)

Use case: Sorting Strings alphabetically

Practice Problems

Problem 1: What is the output?

String s = "Programming"; System.out.println(s.length()); System.out.println(s.substring(0, 7)); System.out.println(s.indexOf("g"));

Answers: 11, "Program", 3

Problem 2: What is the output?

String a = "Cat"; String b = "Dog"; System.out.println(a.compareTo(b)); System.out.println(a.equals(b));

Answers: negative number, false

Problem 3: Fix the error

String word = "Java"; word.toUpperCase(); System.out.println(word); // Expecting "JAVA"

Fix: word = word.toUpperCase();

1 / ?