【发布时间】:2014-09-05 01:13:46
【问题描述】:
如果我在这段代码中使用方法replace,为什么我需要重新定义变量theString:
String theString = "I w@nt to h@ve the regul@r \"A\"!";
theString = theString.replace("@", "a");
System.out.println(theString);
为什么我不能这样做:
theString.replace("@", "a");
就这样?
【问题讨论】:
-
我开始认为 Java 设计者通过调用此方法
replace搞砸了。我们似乎不断地收到同样的问题。也许一个不同的名字会更清楚地表明它通过转换旧字符串来返回一个新字符串。哦,好吧……太晚了…… -
@ajb:只要Java学生一遍又一遍地自言自语,“字符串是不可变的,字符串是不可变的”,那么这应该不是问题。