How I replace multiple chars with only one char in a simple way?
I want to replace some chars with a开发者_开发百科ccents in a String like this example:
str.replace('á','a');
str.replace('â','a');
str.replace('ã','a');
This will work, but I want to know if there is some simple way where I pass all the chars to be replaced and the char that will replace they. Something like these:
replace(str,"áâã",'a');
or:
char[] chars = {'á','â','ã'};
replace(str,chars,'a');
I looked at StringUtils
from Apache Lang
, but not exists this way I mentioned.
You're going to want to look at
str.replaceAll(regex, replacement);
Off the top of my head, I can't recall Java's regex format, so I can't give you a format that catches those three. In my mind, it would be
'[áâã]'
Try .replaceAll()
: str.replaceAll('[áâã]', 'a');
This should work str.replaceAll("[áâã]",'a')
str.replaceChars("áâã", "aaa");
Maybe a simple originalString.replaceAll("á|â|ã", "a")
will do
str.replaceAll("[áâã]","a");
Try this - it's a regex patter saying replace occurrence of each of the characters by "a"
精彩评论