我需要撰写电子邮件正文,并将每个用户的用户名密码属性添加到其中。现在,我将 html 内容放在字符串中,并且尝试使用字符串格式化程序来替换相应用户的用户名和密码。我如何将 html 内容放在单个字符串中,并且还无法相应地更改不同用户的映射。
代码片段
String regEmailBody =
"<div>
+ "successfully registered for the system. <br>Your username "
+ "is[@email_placeholder].<br>Your temporary password is <b>"
+ "@sysGeneratedPW_placeholder!</b><br>Please login to the system to reset your password, "
+ "click on the button below.<br></p></div>";
regEmailBody.replaceFirst("@name_placeholder", name);
regEmailBody.replaceAll("@userEmail", emailAdd);
最佳答案
java中的字符串是不可变的。在您的代码中,您将使用替换的部分生成新字符串,但不会将它们存储在任何地方。
regEmailBody = regEmailBody.replaceFirst("@name_placeholder", name);
(示例代码在 div 后面也缺少一个“,并且占位符字符串与您要替换的内容不对应。)
关于java - 使用正则表达式将 HTML 内容转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23275541/