Java String replace character example shows how to replace a character in the string using the replace and replaceFirst methods of the String class.
How to replace a character in a string?
The Java String class provides the replace
method that takes two char arguments that can be used to replace a character in a string.
1 |
public String replace(char oldCharacter, char newCharacter) |
This method replaces all occurrences of the old character with the new one in the string.
1 2 3 4 5 6 7 8 9 10 11 |
package com.javacodeexamples.stringexamples; public class StringReplaceCharacter { public static void main(String[] args) { String str = "cat"; System.out.println( str.replace('c', 'b') ); } } |
Output
1 |
bat |
As you can see from the output, the character ‘c’ is replaced with the character ‘b’ in the string. Instead of the character version of the replace
method, you can also use the overloaded replace
method that accepts two string arguments instead of the character arguments like given in the below code example.
1 2 |
String str = "cat"; System.out.println( str.replace("c", "b") ); |
Output
1 |
bat |
The replace
method replaces all the old characters in the string with the new one. See below given example.
1 2 |
String str = "cat can"; System.out.println( str.replace("c", "b") ); |
Output
1 |
bat ban |
If you want to replace only the first instance of the character with the new one, you can use the replaceFirst
method instead of the replace
method.
1 |
public String replaceFirst(String regex, String replacement) |
Here is the same example with the replaceFirst
method instead of the replace
method.
1 2 |
String str = "cat can"; System.out.println( str.replaceFirst("c", "b") ); |
Output
1 |
bat can |
As you can see from the output, only the first instance of the character ‘c’ was replaced in the string.
Additionally, the replace
method is case-sensitive. It means that the case of the character to replace must be the same, otherwise, the replacement will not happen.
1 2 |
String str = "Cat"; System.out.println( str.replaceFirst("c", "b") ); |
Output
1 |
Cat |
As you can see from the output since the case of the character we want to replace does not match with the case of the character in the string, no replacement was done. If you want to do the replacement regardless of the case, you can either convert the string to uppercase or to lowercase and then do the replacement as given below.
1 2 |
String str = "Cat"; System.out.println( str.toLowerCase().replaceFirst("c", "b") ); |
Output
1 |
bat |
Lastly, both the replace
and replaceFirst
methods return a new string having replaced content. The original string object remains as-is.
1 2 3 |
String str = "cat"; System.out.println( str.replace("c", "b") ); System.out.println(str); |
Output
1 2 |
bat cat |
If you want the replacement results back in the original string object, you need to assign the return value of these methods to the original string reference as given below.
1 2 3 |
String str = "cat"; str = str.replace("c", "b"); System.out.println(str); |
Output
1 |
bat |
If you want to learn more about the string in Java, please visit the Java String tutorial.
Please let me know your views in the comments section below.