ASP.net c# replace string not working

前端 未结 4 584
灰色年华
灰色年华 2020-12-12 04:24
        // Build email link
        confirmLink = Master.siteDomain + \"/newsLetter.aspx?action=confirm&e=\" + emailAddress + \"&code=\" + verCode;

                 


        
相关标签:
4条回答
  • 2020-12-12 05:06

    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);
    
    0 讨论(0)
  • 2020-12-12 05:07

    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);
    
    0 讨论(0)
  • 2020-12-12 05:13

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

    emailBody = emailBody.Replace("[CONFIRMATION_LINK]", confirmLink);
    
    0 讨论(0)
  • 2020-12-12 05:25

    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);
    
    0 讨论(0)
提交回复
热议问题