HOWTO · Java
How to Replace Multiple Characters in String in Java
This article explains replaceAll, replace, and replaceFirst methods that can be used to replace multiple characters in a string.
On this page
In Java, use String.replaceAll() with a regular-expression character class to replace several selected characters throughout a string. For literal mappings that do not need a pattern, chain String.replace() calls. Use String.replaceFirst() when only the first regex match should change.
Replace Multiple Characters in a String Using replaceAll() in Java
String.replaceAll(regex, replacement) treats its first argument as a regular expression and replaces every match. A character class such as [_/] matches one underscore or one forward slash at a time. It does not match the two-character sequence _/ as a pair.
This complete program changes underscores and slashes to hyphens, then prints both strings:
public class ReplaceAllChars {
public static void main(String[] args) {
String input = "j_u_s_t/a/s/t/r/i/n/g";
String result = input.replaceAll("[_/]", "-");
System.out.println("Input: " + input);
System.out.println("Result: " + result);
}
}
Input: j_u_s_t/a/s/t/r/i/n/g
Result: j-u-s-t-a-s-t-r-i-n-g
In Java source, a backslash for the regex must also be escaped in the string literal. For example, "\\d" supplies the regex \d, which matches a digit. Replacing a match with "" removes it. For example, "abcd12345efgh".replaceAll("\\d", "") removes digits, "s t r i n g".replaceAll(" ", "") removes ordinary spaces, and "s t\\tr".replaceAll("\\s", "-") replaces regex whitespace, including spaces and tabs, with hyphens.
These expressions remove digits, remove ordinary spaces, and replace regex whitespace (including spaces and tabs) with hyphens. To remove lowercase ASCII letters specifically, use [a-z]. Java treats \p{Lower} as ASCII by default; it includes Unicode lowercase characters only when Unicode character class behavior is enabled.
Replace Multiple Characters in a String Using String.replace() in Java
String.replace(target, replacement) matches a literal character sequence, not a regular expression. Each call replaces all occurrences of its target and returns a new string; Java strings are immutable. Chaining is useful when each target has its own mapping, and the calls run from left to right, so an earlier replacement can create text that a later call then matches.
For example, starting with "javbjavcjadakavajavc", the chain input.replace("b", "a").replace("c", "a").replace("d", "v").replace("k", "j") produces javajavajavajavajava. Choose replace() when the targets are literal strings and individual mappings are clearest as separate calls; use replaceAll() when a regex pattern can describe the characters to change together.
Replace Multiple Characters in a String Using String.replaceFirst() in Java
String.replaceFirst(regex, replacement) also takes a regular expression, but changes only the first match per call. Repeating the call applies the next regex search to the string returned by the previous call. This can be useful when the number and order of changes are intentional.
For example, "This is a sample string.".replaceFirst("i", "1").replaceFirst("s", "2") returns Th12 is a sample string. The first call changes the i in This; the second call then changes the first s in the updated string, which is the s in This. Use replaceFirst() for first-match behavior; use replaceAll() when every match should change.
Conclusion
For several selected characters, use a regex character class with String.replaceAll() to change every match. Chain String.replace() calls for literal mappings, and choose String.replaceFirst() when each call should affect only its first regex match.