Replacing multiple occurences of special characters by a single special character

前端 未结 1 1549
南方客
南方客 2020-12-21 08:26

I want to remove multiple occurrences of special characters like \" \", \"-\", \"!\", \"_\" from my java string by a sing

相关标签:
1条回答
  • 2020-12-21 09:02

    Note that \1 is a backreference to the contents matched with the first capturing group. To actually match one or more any characters from the character class, just use a + quantifier:

    [\\s!-]+
    

    So, use

    str = str.replaceAll("[\\s!-]+","_");
    

    See IDEONE demo

    0 讨论(0)
提交回复
热议问题