【发布时间】:2015-02-16 09:39:52
【问题描述】:
我正在寻找有关将空格数字替换为另一个的解决方案,例如:
"My Example is interesting".replaceAll(" ", "1");
不返回"My1Example1is1interesting",而只返回"My"
我也在 StackOverflow 上寻找解决方案,但通常会发现“从字符串/URL 中删除空格..”等。
Scanner s = new Scanner(System.in);
String in = s.next();
in = in.replaceAll("//s+", "1");
System.out.println(in);
【问题讨论】:
-
您的代码中还有其他问题,所以我建议您将其全部发布,以便我们查看发生了什么。当我运行
"My Example is interesting".replaceAll(" ", "1");时,它绝对会返回"My1Example1is1interesting"。 -
如果您的操作与问题中提到的相同,那么它应该可以工作。String.replaceAll(" ","1");是正确的。
-
偏差:你的例子并不有趣。
-
你的帖子也远非有趣:)
-
可能是 String in = s.next();此行导致问题。