Replace Multiple Characters in String in Java
-
Replace Multiple Characters in a String Using
replaceAll()
in Java -
Replace Multiple Characters in a String Using
String.replace()
in Java

String.replaceAll()
and String.replace()
are two useful methods to replace characters in a string in Java. In this article, we will see how we can use these two methods to replace multiple characters in a string.
The replaceAll()
can do this alone using the regular expression, but if we don’t want to use regular expressions, we can use the replace()
method.
Replace Multiple Characters in a String Using replaceAll()
in Java
replaceAll()
is used when we want to replace all the specified characters’ occurrences. We can use regular expressions to specify the character that we want to be replaced. This method takes two arguments, the first is the regular expression pattern, and the second is the character we want to place.
In the following example, we will replace multiple characters using some common regular expressions.
public class ReplaceAllChars {
public static void main(String[] args) {
String stringUnderscoresForward = "j_u_s_t_a_s/t/r/i/n/g";
String stringWithDigits = "abcd12345efgh";
String stringWithWhiteSpaces = "s t r i n g";
String stringWithLowerCase = "This is a Lower Case String";
String finalString1 = stringUnderscoresForward.replaceAll("[_/]", "-");
String finalString2 = stringWithDigits.replaceAll("[\\d]", "");
String finalString3 = stringWithWhiteSpaces.replaceAll("[ ]", "");
String finalString4 = stringWithWhiteSpaces.replaceAll("[\\s]", "-");
String finalString5 = stringWithLowerCase.replaceAll("[\\p{Lower}]", "");
System.out.println("Old String: "+stringUnderscoresForward+" New String: "+finalString1);
System.out.println("Old String: "+stringWithDigits+" New String: "+finalString2);
System.out.println("Old String: "+stringWithWhiteSpaces+" New String: "+finalString3);
System.out.println("Old String: "+stringWithLowerCase+" New String: "+finalString4);
System.out.println("Old String: "+stringWithLowerCase+" New String: "+finalString5);
}
}
Output:
Old String: j_u_s_t_a_s/t/r/i/n/g --New String: j-u-s-t-a-s-t-r-i-n-g
Old String: abcd12345efgh --New String: abcdefgh
Old String: s t r i n g --New String: string
Old String: This is a Lower Case String --New String: s-t-r-i-n-g
Old String: This is a Lower Case String --New String: T L C S
In the above example, we use multiple regular expressions that are commonly used. Let us see what they mean and how they work.
stringUnderscoresForward
has every character separated by underscores and forward slashes; we will replace all of them with a dash ( -
). [char1 char2]
is the regex used to replace two characters with a single character. We can use [_/]
to replace all the underscores and slashes with a dash.
stringWithDigits
is a string with random alphabets and some digits in between them. We want to replace every digit with an empty character. To do this, we can use the \d
escape sequence that escapes the digits. [\\d]
will be used as a regex, and the replacing character will be an empty character.
stringWithWhiteSpaces
contains whitespaces between every character. To remove the spaces, we can replace them with empty characters. Empty brackets with white space [ ]
indicate a white space in the string.
We can also use [\\s]
to get the whitespaces in a string.
stringWithLowerCase
has both the lower and upper case characters. We want to replace every lower case character with an empty character. We will use [\\p{Lower}]
which is a regex to get all the lower case characters.
Replace Multiple Characters in a String Using String.replace()
in Java
public class ReplaceAllChars {
public static void main(String[] args) {
String stringWithRandomChars = "javbjavcjadakavajavc";
String finalString = stringWithRandomChars
.replace("b", "a")
.replace("c", "a")
.replace("d", "v")
.replace("k", "j");
System.out.println(finalString);
}
}
Output:
javajavajavajavajava
Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.
LinkedInRelated Article - Java String
- Perform String to String Array Conversion in Java
- Remove Substring From String in Java
- Convert Byte Array in Hex String in Java
- Convert Java String Into Byte
- Generate Random String in Java
- The Swap Method in Java