In this tutorial, you will learn about String replace() method, its syntax, and usage with examples.

Java String replace() method

In Java, String replace() method is used to replace all the occurrences of an old character with a new character in the string, or replace all the occurrences of a target string with a new replacement string.

Syntax of replace()

There are two replace() methods defined in String class. Based on the type of arguments, respective method is called.

public int replace(char oldChar, char newChar)
public int replace(CharSequence target, CharSequence replacement)

The syntax to call String replace() method is

string.replace(oldChar, newChar)
// or
string.replace(target, replacement)

where

ParameterDescription
oldChar[Mandatory]A char value.The value which is to be searched for in the string, to replace.
newChar[Mandatory]A char value.The new character which shall be used as replacement for the old character in the string.
target[Mandatory]A String value.The value which is to be searched for in the string, to replace.
replacement[Mandatory]A String value.The new character which shall be used as replacement for the old character in the string.

replace(oldChar, newChar) returns a new string with all the occurrences of oldChar replaced with newChar in the string.

replace(target, replacement) returns a new string with all the occurrences of target string replaced with replacement string in the string.

The following are some of the examples to call replace() method on str string with valid arguments.

// replace(oldChar, newChar)
str.replace('m', 'p')

// string.replace(target, replacement)
str.replace("apppleee", "apple")
ADVERTISEMENT

Examples

1. replace(oldChar, newChar) – Replace a character in string in Java

In this example, we are given a string value in str, and we have to replace all the occurrences of the old character 'm' with the new character 'p'. We shall use replace() method of String class.

Java Program

public class Main {
	public static void main(String[] args) {
		String str = "ammle is good";
		String result = str.replace('m', 'p');
		System.out.println("Given string  : " + str);
		System.out.println("Result string : " + result);
	}
}

Output

Given string  : ammle is good
Result string : apple is good

Explanation

a m m l e   i s   g o o d    ← given string
  ↓ ↓  replace 'm' with 'p'
  p p
a p p l e   i s   g o o d    ← result string

2. replace(target, replacement) – Replace a target string with replacement string

In this example, we are given a string value in str, and we have to replace all the occurrences of the target string "apppleee" with the replacement string "apple". We shall use replace() method of String class.

Java Program

public class Main {
	public static void main(String[] args) {
		String str = "There are many apppleees in the apppleee basket.";
		String result = str.replace("apppleee", "apple");
		System.out.println("Given string  : " + str);
		System.out.println("Result string : " + result);
	}
}

Output

Given string  : There are many apppleees in the apppleee basket.
Result string : There are many apples in the apple basket.

Explanation

There are many apppleees in the apppleee basket.   ← given string
               apppleee         apppleee
               ↓                ↓  replace "apppleee" with "apple"
There are many apples in the apple basket.         ← result string

Conclusion

In this Java String Methods tutorial, we have seen about String replace() method in Java, its syntax, and how to use String replace() method in Java programs with the help of examples.