Java ProgramsStringsReplace Word in a String

Replace Word in a String in Java

beginner·  Strings  ·  String Manipulation

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.

Input
"I love Java. Java is fun.", "Java" → "Python"
Output
I love Python. Python is fun.

Java Program

Java
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

I love Python. Python is fun.

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.

How It Works
  1. 1str.indexOf(target, index) searches for the next occurrence of the target word, starting from position index.
  2. 2result.append(str, index, found) copies the untouched stretch of text between the last match and this one.
  3. 3replacement is appended in place of the matched target word.
  4. 4index is 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.
For "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.

Complexity
Time Complexity: O(n × m)Space Complexity: O(n)

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

String.indexOf()StringBuilderwhile loop

Approach 2: Using replace()

Java
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

I love Python. Python is fun.

Core Logic

In real code, there's no reason to loop manually — replace() already swaps every occurrence of a word in one call.

How It Works
  1. 1str.replace(target, replacement) takes the word to find and the word to substitute in its place.
  2. 2Internally, it performs the same kind of scan-and-copy as the manual version.
  3. 3No explicit loop or StringBuilder is 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.

Complexity
Time Complexity: O(n × m)Space Complexity: O(n)

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.

Key Concepts

String.replace()

Related Programs