Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
你能告诉我有没有办法在java或spring中用单个空格替换多个空格?任何 stringUtils 功能相同吗?
喜欢
1.
test test test test
2.
3.
替换多个空格
output = input.replaceAll("[ ]+", " ");
或者替换多个空白字符(包括空格、制表符、换行符等)
output = input.replaceAll("\\s+", " ");
这是@pswg 的变体——它忽略换行符,但仍会得到制表符等。
output = input.replaceAll("[^\\S\\r\\n]+", " ");