不能更换到另一个字符串(布尔值) - Java的 [英] Cannot to replace a string to another (boolean value) - Java
问题描述
boolean b1 = false;
boolean b2 = true;
String s = new String(b1+""+b2);
s.replaceAll("false", "f");
s.replaceAll("true", "t");
没有被替换。我仍然得到假真。
我要全部替换假与真的字符串,以F和t。
Nothing is replaced. I still get "false true". I want to replace all "false" and "true", of the String, to "f" and "t".
林pretty确定我不是在做正确的方式,而这就是为什么我需要你的帮助,这将大大AP preciated。
Im pretty sure Im not doing it the right way, and thats why I need your help, which will be greatly appreciated.
推荐答案
字符串在Java中是不可变的,这意味着,一旦创建,一个String对象的值不会改变。
String in java is immutable, which means, once created, the value of a String object will not change.
该方法的replaceAll()(或String类的几乎所有其他方法),因此被设计为返回一个新的字符串,而不是修改旧的。
The method replaceAll() (or almost all other methods in String class) hence are designed to return a new string rather than modifying the old one.
因此,呼叫应被制成
So the call should be made as
s = s.replaceAll("false", "f");
更多关于不变性是这里
这篇关于不能更换到另一个字符串(布尔值) - Java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!