Strings and text operations
1.5.2 — Strings and text operations
A String holds text. This lesson compares, searches, and changes the way text is written.
String name = "Ada"; stores a reference to a String. System.out.println(...) prints a value. true and false are boolean results. Every code cell includes the text it uses.
Compare the text, not the reference
A reference identifies an object. == checks whether two String references identify the same object. equals(...) checks whether their text matches.
new String("Ada") deliberately creates a separate String object in the next example. Both objects contain Ada, so equals is true even though == is false.
Use equals when you want to compare text. Quoted strings can share an object, so a successful == check would not make it a reliable text comparison.
Length and part of a String
For the simple text in this lesson, length() gives the number of letters.
substring(start, end) returns the text from the start index up to, but not including, the end index. Indexes start at 0.
| Index | Letter in River |
|---|---|
| 0 | R |
| 1 | i |
| 2 | v |
| 3 | e |
| 4 | r |
substring(0, 3) takes indexes 0, 1, and 2. Bounds must satisfy 0 <= start <= end <= length().
API reference, in plain language: substring includes the start index and excludes the end index; an invalid range causes an IndexOutOfBoundsException. For general text, length counts UTF-16 code units, which are not always whole displayed characters. The simple letters used here each take one unit. [String API](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/String.html#substring(int,int)).
Search inside text
contains(...) reports whether text is present. indexOf(...) gives the first matching index, or -1 if there is no match.
These operations are case-sensitive: "Walk" and "walk" are different text.
Split a simple comma-separated value
split(",") separates text at commas and returns a String array. The comma itself is not included.
An enhanced for loop, for (String part : parts), reads each array value in order. This example uses plain names without embedded commas or quotes; it is not a full CSV parser. By default, trailing empty parts are omitted.
String text does not change in place
Strings are immutable: their contents cannot be modified. Methods such as trim() return a result instead of changing the original String.
trim() removes leading and trailing ordinary spaces. The next cell saves its result in a separate variable. Square brackets in the output make the spaces visible.
Store the cleaned text
To keep a changed spelling, assign the result. toLowerCase() converts letters to lowercase. For these simple names, combining it with trim() gives a consistent spelling.
Calling methods one after another uses the result of the previous call. Here, trim() runs first and toLowerCase() uses the trimmed text.
StringBuilder changes its own text
A StringBuilder is useful when building text in several steps. append(...) adds to the same builder. toString() gives you a String with its current text.
Unlike String operations, the append calls below modify the builder itself.
Practice: remove surrounding spaces
Replace practiceName on the right of the second line with practiceName.trim().
Run the cell. The output should be [Ada], with no spaces inside the brackets.
Hint
The second line should be String practiceClean = practiceName.trim();.
Worked solution
String practiceName = " Ada ";
String practiceClean = practiceName.trim();
System.out.println("[" + practiceClean + "]");Try different text
In your completed cell, change only the value assigned to practiceName:
| practiceName | Expected output |
|---|---|
"Ben" | [Ben] |
" Ben " | [Ben] |
" " | [] |
"" | [] |
The original text can be empty; trim still produces a usable String.
Enable JavaScript to run the Java notebook.