Sub topic 7
Creating Transformed Strings
A string is a text value. You can transform text by making a new string with changes, such as removing spaces or changing letters to uppercase.
Java strings are immutable, which means their contents cannot change after they are created. A transformation leaves the original string alone and returns a new one.
A method is an action you can call on a value. Its return value is the result it gives back. To keep a transformed string, save that return value in a variable.
Common transformations
| Method | What it returns |
|---|---|
| strip() | Text with whitespace removed from both ends |
| toUpperCase() | Text with letters changed to uppercase |
| toLowerCase() | Text with letters changed to lowercase |
| replace(old, new) | Text with matching parts replaced |
Whitespace includes spaces, tabs, and line breaks. The strip() method does not remove spaces between words.
The replace() method treats its inputs as literal text, not as a search pattern. It replaces every matching part and is case-sensitive: “Ada” does not match “ada”.
Worked example
This program cleans up a name, replaces part of it, and makes a lowercase tag. It also shows what happens when you do not save a return value.
Save it as TransformText.java. It needs Java 11 or later because it uses strip().
import java.util.Locale;
class TransformText {
public static void main(String[] args) {
String original = " Ada Lovelace ";
String clean = original.strip();
String changed = clean.replace("Ada", "Maya");
String tag = changed.replace(" ", "-").toLowerCase(Locale.ROOT);
clean.toUpperCase(Locale.ROOT);
System.out.println("original: [" + original + "]");
System.out.println("clean: [" + clean + "]");
System.out.println("changed: [" + changed + "]");
System.out.println("tag: [" + tag + "]");
}
}Compile and run it:
javac TransformText.java
java TransformTextThe output is:
original: [ Ada Lovelace ]
clean: [Ada Lovelace]
changed: [Maya Lovelace]
tag: [maya-lovelace]The brackets make the spaces around the original text visible. The original still has its spaces because strip() returned a new string. The clean string still says “Ada” because replace() also returned a new string.
The tag line calls two methods in a row. The first result has hyphens instead of spaces. The second result has lowercase letters.
The call to clean.toUpperCase(Locale.ROOT) creates an uppercase string, but the program does not save it. As a result, clean remains “Ada Lovelace”.
Locale.ROOT tells Java to use language-independent case rules. This is useful for a tag like the one above. Text meant for people to read may need rules for their language instead.
Keep the result
You can assign a result back to the same variable:
class SaveResult {
public static void main(String[] args) {
String word = " hello ";
word = word.strip();
System.out.println(word);
}
}This prints “hello”. The assignment makes word refer to the new string; it does not change the old string.
Exercise
What does String result = " red red ".strip().replace("red", "blue"); put in result? Does it change the original text?
Show answer
result is "blue blue". The original text is not changed; each method returns a new string.