final String message =
"Please enter the following code in the password reset screen:\n" +
"\n"+
"{CODE} (((((" + codeStr + ")))))\n" +
"If you didn't ask for this code, or don't know what this e-mail is about, you can safely ignore it."
.replace("{CODE}", codeStr);
这会导致
Please enter the following code in the password reset screen: {CODE} (((((5RE2GT4FWH))))) If you didn't ask for this code, or don't know what this e-mail is about, you can safely ignore it.
这么简单的替换怎么会不起作用呢?我认为问题可能在于 {CODE}
的两个实例看起来很相似,但实际上由不同的字符组成,但我将一个复制粘贴到另一个上,但没有解决问题。
最佳答案
问即知:
final String message =
(
"Please enter the following code in the password reset screen:\n" +
"\n"+
"{CODE}" + codeStr + "\n" +
"If you didn't ask for this code, or don't know what this e-mail is about, you can safely ignore it."
)
.replace("{CODE}", codeStr);
由于缺少括号,所以先对组成字符串的最后部分进行替换,然后才将各部分连接在一起。
关于java - 非常简单的字符串替换失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9740129/