Java String replace Example shows how to replace string with other string in Java. The example also shows how to replace a character, first occurrence or all occurrences of the string in Java.
How to replace a character in String in Java?
To replace a character in the string, you can use the replace
method of Java String class.
1 |
public String replace(char oldCharacter, char newCharacter) |
This method replaces all the occurrences of the specified character with a new character and returns the result in a new String.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.javacodeexamples.stringexamples; public class StringReplaceExample { public static void main(String[] args) { //String replace character String str = "Java Example"; str = str.replace('J', 'L'); System.out.println(str); } } |
Output
1 |
Lava Example |
Note: If the specified character does not exist in the String, then the reference to the original string is returned. Otherwise, a new String object is created as a result of replacement and returned.
Please also note that, since the string is immutable (cannot be changed once created), the replace
method does not change the original String but returns a new String as given below.
1 2 3 4 |
String str = "Java Example"; str.replace('J', 'L'); System.out.println(str); |
Output
1 |
Java Example |
Make sure to assign the result of the replace
method back to the original String if you want the result in the original string.
How to replace string with another string?
To replace string with another string, you can use the replace
method of String class.
1 |
public String replace(String find, String replacement) |
This method replaces all the occurrences of a specified string with the replacement string and returns result in a new String.
Example
1 2 3 4 5 |
//replace String String str = "Java Example"; str = str.replace("Java", "C++"); System.out.println(str); |
Output
1 |
C++ Example |
If the string does not contain the specified string, the reference of the original string is returned. Otherwise, a new string is created as a result of replacement and returned.
Important note: Even though the name of the method is “replace”, it replaces ALL the occurrences of the specified string in a given string, not just one as given below.
1 2 3 4 5 |
//replace all occurrences of String String str = "Java Example, Java String replace Example"; str = str.replace("Java", "C++"); System.out.println(str); |
Output
1 |
C++ Example, C++ String replace Example |
How to replace the first occurrence of a string with other string in Java?
To replace only the first occurrence of a string within the string, use the replaceFirst
method.
1 |
public String replaceFirst(String regex, String replacement) |
This method replaces the first substring that matches the regular expression with the replacement. It returns the result in a new string object.
1 2 3 4 5 |
//replace first occurrence of String String str = "Java Example, Java String replace Example"; str = str.replaceFirst("Java", "C++"); System.out.println(str); |
Output
1 |
C++ Example, Java String replace Example |
Important Note: The replaceFirst
method accepts regular expression while the replace
method does not. For example, below given code replaces the first digit of the string with the empty string using a regular expression.
1 2 3 4 5 |
//replace first digit with empty string String str = "007 James Bond"; str = str.replaceFirst("[0-9]", ""); System.out.println(str); |
Output
1 |
07 James Bond |
If you want to specify a regular expression, you should use the replaceAll
method instead.
How to replace a string with Apache Commons library?
If you are using the Apache Commons library, you can use the replace
method of the StringUtils class as given below.
1 2 3 4 |
String str = "Apache Commons Java"; str = StringUtils.replace(str, "Java", "Rocks"); System.out.println(str); |
Output
1 |
Apache Commons Rocks |
This example is a part of the String in Java tutorial.
Please let me know your views in the comments section below.