String: How to replace multiple possible characters with a single character?

后端 未结 3 1772
野趣味
野趣味 2020-12-25 10:11

I would like to replace all \'.\' and \' \' with a \'_\'

but I don\'t like my code...

is there a more efficient way to

3条回答
  •  一向
    一向 (楼主)
    2020-12-25 10:45

    String new_s = s.toLowerCase().replaceAll("[ .]", "_");
    

    EDIT:

    replaceAll is using regular expressions, and using . inside a character class [ ] just recognises a . rather than any character.

提交回复
热议问题