Replace Word in a String in Java
Problem
Replacing a word means swapping every occurrence of one word or phrase in a string for a different one, leaving the rest of the text untouched.
Given a string and a target word, replace every occurrence of that word with a replacement word.
Java Program
public class ReplaceWord {
public static void main(String[] args) {
String str = "I love Java. Java is fun.";
String target = "Java";
String replacement = "Python";
StringBuilder result = new StringBuilder();
int index = 0;
int found;
while ((found = str.indexOf(target, index)) != -1) {
result.append(str, index, found); // copy the untouched part before the match
result.append(replacement);
index = found + target.length();
}
result.append(str.substring(index)); // copy whatever remains after the last match
System.out.println(result.toString());
}
}Output
Core Logic
Repeatedly finding the next occurrence of the target, copying everything up to it, and inserting the replacement instead, rebuilds the string one match at a time.
- 1
str.indexOf(target, index)searches for the next occurrence of the target word, starting from positionindex. - 2
result.append(str, index, found)copies the untouched stretch of text between the last match and this one. - 3
replacementis appended in place of the matched target word. - 4
indexis advanced past the match, so the next search starts after it — and once no more matches are found, whatever text remains is appended as-is.
"I love Java. Java is fun." with target "Java", both occurrences are swapped for "Python", producing "I love Python. Python is fun.".Key Point: This copies the string in chunks between matches, rather than character by character — the same overall idea as counting substring occurrences, but building a new string instead of a count.
Why: Each search for the next occurrence may compare up to m characters at every position, and the result buffer grows to hold the final replaced string.
Key Concepts
Approach 2: Using replace()
public class ReplaceWordBuiltin {
public static void main(String[] args) {
String str = "I love Java. Java is fun.";
// replace() swaps every occurrence of the target word in one call
System.out.println(str.replace("Java", "Python"));
}
}
Output
Core Logic
In real code, there's no reason to loop manually — replace() already swaps every occurrence of a word in one call.
- 1
str.replace(target, replacement)takes the word to find and the word to substitute in its place. - 2Internally, it performs the same kind of scan-and-copy as the manual version.
- 3No explicit loop or
StringBuilderis needed in your own code.
"I love Java. Java is fun.".replace("Java", "Python") returns "I love Python. Python is fun." in a single call.Key Point: This is the version to actually use — the manual loop exists only to show what replace() is conceptually doing under the hood.
Why: replace() still has to scan the whole string and build a new one with the substitutions made, but that work happens inside the JDK instead of your own loop.