【发布时间】:2019-01-25 09:09:11
【问题描述】:
考虑:
System.out.println(new String(new char[10]).replace("\0", "hello"));
有输出:
hellohellohellohellohellohellohellohellohellohello
但是:
System.out.println(new String(new char[10]).replace("", "hello"));
有输出:
hello hello hello hello hello hello hello hello hello hello
这些多余的空间是从哪里来的?
【问题讨论】:
-
你应该遍历字符串并打印每个字符。
-
@Ferrybig 从来没有尾随空格。 OP的原始描述(可能)不正确。参见例如ideone.com/492Cml。