How to simply replace multiple characters with just one char?

I want to replace some characters with accents in 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 easy way to pass all the characters that need to be replaced, and the char that will replace them. Something like that:

 replace(str,"รกรขรฃ",'a'); 

or

 char[] chars = {'รก','รข','รฃ'}; replace(str,chars,'a'); 

I looked at StringUtils from Apache Lang , but not the way I mentioned.

+4
source share
6 answers

Would you like to watch

 str.replaceAll(regex, replacement); 

At the top of my head, I can't remember the Java regex format, so I can't give you a format that catches these three. In my opinion, that would be

 '[รกรขรฃ]' 
+2
source

Try .replaceAll() : str.replaceAll ('[รกรขรฃ]', 'a');

+1
source

This should work str.replaceAll ("[รกรขรฃ]", 'a')

0
source
 str.replaceChars("รกรขรฃ", "aaa"); 
0
source

Perhaps a simple originalString.replaceAll("รก|รข|รฃ", "a") will do

0
source
 str.replaceAll("[รกรขรฃ]","a"); 

Try this - this is a regular expression talking about replacing the appearance of each character with "a"

0
source

All Articles