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

String replace with integer not working

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

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

    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' 的新字符串并将其打印出来 'TIME: value'。 我正在更改的原始字符串是:事件现在是 -time- 分钟!

这里的问题是 System.out.println("TIME:") 显示了正确的值,但是 .replace 不起作用。 我无法解决这个问题,有人知道吗?

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

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