ASP.net c# replace string not working

主宰稳场 提交于 2019-11-28 14:28:26

Strings are immutable. String operation generally return new string instances. Try this:

emailBody = emailBody.Replace("[CONFIRMATION_LINK]", confirmLink);

The Replace method returns a new, updated string. You need to assign the results of the Replace call back to emailBody:

emailBody = emailBody.Replace("[CONFIRMATION_LINK]", confirmLink);

Strings in C# are immutable, meaning they cannot be changed once they are instantiated unless explicitly pointed to another string. What happens is that you are calling "Replace" without any variable to receive the string that returns.

Change the 2nd to the last line with this code:

emailBody = emailBody.Replace("[CONFIRMATION_LINK]", confirmLink);

It's because Replace does not edit the emailBody variable. It returns the resultant string. In this case, you'd need:

string bodyToSend = emailBody.Replace("[CONFIRMATION_LINK]", confirmLink);
emailer.sendEmail(emailAddress, Master.noReplyEmail, emailSubj, bodyToSend);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!