我编写了一些代码并使用了一个字符串,我使用+ =连接(因为我只做了几次.
后来我使用了另一个字符串并使用了concat()函数.连接不起作用.
所以我在Junit中写了一个小方法(用eclipse)......
@Test
public void StingConcatTest(){
String str = "";
str += "hello ";
str += " and goodbye";
String conc="";
conc.concat("hello ");
conc.concat(" and goodbye");
System.out.println("str is: " + str + "\nconc is: "+ conc);
Run Code Online (Sandbox Code Playgroud)
输出是......
str is: hello and goodbye
conc is:
Run Code Online (Sandbox Code Playgroud)
所以要么我疯了,我做错了(最有可能),JUNIT有问题,或者我的JRE/eclipse有问题.
请注意,stringbuilders工作正常.
大卫.
好的,我们每天至少看几次这个问题.
Strings是的immutable,所以String上的所有操作都会产生新的String.
conc= conc.concat("hello "); 你需要再次将结果重新分配给字符串