字符串:如何用单个字符替换多个可能的字符? [英] String: How to replace multiple possible characters with a single character?
本文介绍了字符串:如何用单个字符替换多个可能的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用'_'
但是我不喜欢我的代码...
but I don't like my code...
是否有比这更有效的方法:
is there a more efficient way to do this than:
String new_s = s.toLowerCase().replaceAll(" ", "_").replaceAll(".","_");
?
toLowerCase()就在那里,因为我也希望它也小写...
toLowerCase() just there because I want it lower-cased as well...
推荐答案
String new_s = s.toLowerCase().replaceAll("[ .]", "_");
replaceAll
is using regular expressions, and using .
inside a character class [ ]
just recognises a .
rather than any character.
这篇关于字符串:如何用单个字符替换多个可能的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文