Introduction
The Java String class offers several methods for replacing characters or substrings within a string. The `replace()` method is one such tool, allowing you to substitute specific parts of your string with new ones. This tutorial focuses on the two variations of the `replace()` method: one that replaces individual characters and another that replaces character sequences.
Syntax
public String replace(char oldChar, char newChar)
public String replace(CharSequence target, CharSequence replacement)
Parameters Table
| Parameter |
Description |
oldChar |
The character to be replaced. |
newChar |
The new character that will replace oldChar. |
target |
The sequence of characters to be replaced. |
replacement |
The replacement sequence for the target. |
Return Value
Both versions of replace() return a new string with the replacements made. The original string remains unchanged, as strings are immutable in Java.
Examples
Replacing Single Characters
This example demonstrates how to use `replace()` to replace all occurrences of a specific character within a string.
public class ReplaceCharExample {
public static void main(String[] args) {
String original = "Hello World";
char oldChar = 'l';
char newChar = 'x';
String replaced = original.replace(oldChar, newChar);
System.out.println("Original string: " + original);
System.out.println("Replaced string: " + replaced);
}
}
Original string: Hello World
Replaced string: Hexxo Worxd
The code replaces all instances of the character 'l' with 'x' in the string "Hello World". Notice that `replace()` doesn't modify the original string; it creates a new one.
Replacing Character Sequences
This example demonstrates replacing a sequence of characters (a substring) with another sequence using the second version of replace().
public class ReplaceSequenceExample {
public static void main(String[] args) {
String original = "This is a test string. This is.";
CharSequence target = "This ";
CharSequence replacement = "That ";
String replaced = original.replace(target, replacement);
System.out.println("Original string: " + original);
System.out.println("Replaced string: " + replaced);
}
}
Original string: This is a test string. This is.
Replaced string: That is a test string. That is.
Here, we replace all occurrences of the substring "This " with "That ". The `CharSequence` interface allows us to use strings or other character sequences as parameters.
Replacing Only the First Occurrence
To replace only the first occurrence of a sequence, you can combine replace() with substring() and indexOf(). This is not directly supported by `replace()` itself, but it’s a common requirement.
public class ReplaceFirstOccurrenceExample {
public static void main(String[] args) {
String original = "This is a test string. This is.";
CharSequence target = "This ";
CharSequence replacement = "That ";
int index = original.indexOf(target);
if (index != -1) {
String replaced = original.substring(0, index) + replacement + original.substring(index + target.length());
System.out.println("Original string: " + original);
System.out.println("Replaced string: " + replaced);
} else {
System.out.println("Target sequence not found.");
}
}
}
Original string: This is a test string. This is.
Replaced string: That is a test string. This is.
The code finds the index of the first occurence using indexOf(), and then constructs a new string by concatenating substrings before and after the replaced part.
Important Considerations
- Immutability: Remember that Java strings are immutable. The `replace()` method always returns a *new* string; it doesn't modify the original one.
- Regular Expressions (Advanced): For more complex pattern-based replacements, consider using the
replaceAll() method which utilizes regular expressions.

About the authorMallikarjuna Mallisetty
General programming
Mallikarjuna shares practical programming tutorials and foundational concepts designed to help developers learn by building and experimenting.
View LinkedIn profile ↗