gpt4 book ai didi

java - String.replaceAll 奇怪的行为

转载 作者:搜寻专家 更新时间:2023-10-30 19:53:41 25 4
gpt4 key购买 nike

String s = "hi                  hello";
s = s.replaceAll("\\s*", " ");
System.out.println(s);

我有上面的代码,但我不知道为什么会产生

 h i  h e l l o 

而不是

 hi hello

非常感谢

最佳答案

使用+量词来匹配1个或多个空格而不是*:-

s = s.replaceAll("\\s+", " ");

\\s*表示匹配0个或多个空格,会匹配每个字符前的一个空字符,并被空格替换。

关于java - String.replaceAll 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13960302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com