字符串替换为整数不起作用

时间:2015-06-07 21:13:34

标签: java string replace integer replaceall

我一直在研究一些Java代码,我遇到了一个非常奇怪的错误,我试图用Integer替换一段字符串。 添加了各种调试,但我似乎无法找到它。

private void executeMsg() {
    value = value.replaceAll("(&([a-f0-9]))", "§$2");

    String border = tostiuhc.getWorldManager().getBorderSize() - tostiuhc.getShrinksize() + "";
    String time = tostiuhc.getPhase().getMinutes() + "";

    System.out.println("BORDER: " + border);
    System.out.println("TIME: " + time);

    value = value.replace("-border-", border + "");
    value = value.replace("-time-", time + "");

    tostiuhc.broadcast(value + " " + time);
}

正如您所看到的,我创建了一个名为'time'的新String,并将其打印出来'TIME:value'。 我正在改变的原始字符串是:事件现在是-time-分钟!

enter image description here

enter image description here

这里的问题是System.out.println(“TIME:”)显示正确的值,但.replace只是DOESNT工作。 我无法理解这一点,任何人都有任何想法?

1 个答案:

答案 0 :(得分:5)

替换仅适用于第一次执行。之后,value不再包含-time-。作为解决方案,您可以使用临时变量进行替换:

String displayValue = value.replace("-time-", time + "");