我有密码
String txt = "<p style=\"margin-top: 0\">";
txt.replace("style=\"margin-top: 0\"","class=\"style_" + i + "\"");
在 for 循环中(这就是 i 的作用),但是当我运行它时,没有任何内容被替换。我用错了吗?
最佳答案
它应该是这样的:
String txt = "<p style=\"margin-top: 0\">";
txt = txt.replace("style=\"margin-top: 0\"","class=\"style_" + i + "\"");
“String”是一种不可变类型,这意味着 String 上的方法不会改变 String 本身。更多信息在这里 - http://en.wikipedia.org/wiki/Immutable_object .
关于java - 我想我在这里遗漏了一些东西——string.replace(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4263576/