How I replace multiple chars with only one char in a simple way?

10,415

Solution 1

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

'[áâã]'

Solution 2

Try .replaceAll(): str.replaceAll('[áâã]', 'a');

Share:
10,415
Renato Dinhani
Author by

Renato Dinhani

Solving real problems.

Updated on June 21, 2022

Comments

  • Renato Dinhani
    Renato Dinhani almost 2 years

    I want to replace some chars with accents 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.