Welcome to String Manipulation!

In the world of computer science, a String is just a fancy name for a sequence of characters, like "Hello", "APCS-A", or even a blank space " ". While we have used simple numbers (integers and doubles), String manipulation is where programming gets really exciting because it allows us to handle text, names, and messages!

In this chapter, we will learn how to "talk" to String objects using specific methods. Don't worry if this seems like a lot to memorize at first—think of these methods as a "toolbox" that you'll get better at using with every practice problem.

Note: Strings are objects, not primitive types (like int or double). This means they have special powers called methods!

The Golden Rule: Zero-Based Indexing

Before we touch the methods, you must remember the most important rule in Java: We start counting at 0.

If we have the String "JAVA":
The 'J' is at index \(0\).
The 'A' is at index \(1\).
The 'V' is at index \(2\).
The 'A' is at index \(3\).
The length is \(4\), but the last index is \(3\). The last valid index is always \(length - 1\).


1. Creating Strings and Finding Their Length

You can create a String using a constructor: String name = new String("Java"); or simply by using quotes: String name = "Java";

int length()

The length() method returns the total number of characters in the String, including spaces and punctuation.

Example:
String greeting = "Hello!";
int size = greeting.length(); // size is \(6\)

Quick Review: An empty string "" has a length of \(0\).


2. Searching for Characters

int indexOf(String str)

Think of this as the "Where is it?" method. It searches for the first occurrence of str within your String and returns the index where it starts.

Important Behaviors:
1. If the string is found, it returns the index of the first character.
2. If the string is NOT found, it returns \(-1\). This is a very common AP exam question!

Example:
String phrase = "Blueberry";
int find1 = phrase.indexOf("blue"); // Returns \(-1\) (Wait, why? Because Java is case-sensitive!)
int find2 = phrase.indexOf("berry"); // Returns \(4\)


3. Extracting Parts of a String (The Scissors)

The substring method allows you to "cut out" a piece of a String. There are two ways to use it:

String substring(int from, int to)

This returns a slice of the string starting at the from index and stopping right before the to index.

The Trick: The character at index to is NOT included. You can think of it as: \( [from, to) \).

Example:
String word = "Computer";
String part = word.substring(0, 3); // Returns "Com" (Indices \(0, 1,\) and \(2\))

String substring(int from)

This returns a slice starting at the from index and goes all the way to the very end of the string.

Example:
String word = "Computer";
String end = word.substring(4); // Returns "uter" (Starting at index \(4\) to the end)

Common Mistake: If you provide an index that is negative or greater than the length of the string, Java will throw an error (StringIndexOutOfBoundsException). Be careful!


4. Comparing Strings

In Java, we never use \(==\) to compare the content of two Strings. Instead, we use these methods:

boolean equals(Object other)

Returns true if the two strings have the exact same characters in the exact same order; otherwise, returns false.

int compareTo(String other)

This is used for alphabetical (dictionary) ordering. It returns an integer:
- A negative value if the first string comes before other alphabetically.
- Zero if the strings are exactly the same.
- A positive value if the first string comes after other alphabetically.

Mnemonic: "Apple".compareTo("Banana") is negative because Apple comes first!


5. Splitting Strings

String[] split(String del)

The split method "chops" a string into an array of smaller strings based on a "delimiter" (the character where you want to cut).

Example:
String list = "red,blue,green";
String[] colors = list.split(",");
// colors[0] is "red", colors[1] is "blue", colors[2] is "green"


Key Takeaways for the AP Exam

  • Indices: Always start at \(0\). The last index is \(length() - 1\).
  • substring(a, b): Includes index \(a\), excludes index \(b\). The length of the resulting substring is always \(b - a\).
  • indexOf: Returns \(-1\) if the string isn't found. This is often used in "if" statements to check if a word exists.
  • Comparison: Use .equals() for equality and .compareTo() for sorting.
  • Concatenation: You can combine strings or a string and a number using the \(+\) operator (e.g., "Score: " + 10 results in "Score: 10").

Don't worry if this seems tricky at first! String manipulation is all about practice. Try tracing the indices with a pencil and paper—it helps every time!