Vb.net 为什么可以';在IO.File.ReadAllText()字符串上执行String.Replace()吗?
我正在使用System.IO.FIle.ReadAllText()获取我为电子邮件内容创建的一些模板文件的内容。然后,我想对文件中的某些标记进行替换,以便向模板添加动态内容 这是我的代码,在我看来它应该工作得很好Vb.net 为什么可以';在IO.File.ReadAllText()字符串上执行String.Replace()吗?,vb.net,string,.net,Vb.net,String,.net,我正在使用System.IO.FIle.ReadAllText()获取我为电子邮件内容创建的一些模板文件的内容。然后,我想对文件中的某些标记进行替换,以便向模板添加动态内容 这是我的代码,在我看来它应该工作得很好 Dim confirmUrl As String = Request.ApplicationPath & "?v=" & reg.AuthKey Dim text As String = IO.File.ReadAllText( _ ConfigurationMa
Dim confirmUrl As String = Request.ApplicationPath & "?v=" & reg.AuthKey
Dim text As String = IO.File.ReadAllText( _
ConfigurationManager.AppSettings("sign_up_confirm_email_text").Replace("~", _
Request.PhysicalApplicationPath))
Dim html As String = IO.File.ReadAllText( _
ConfigurationManager.AppSettings("sign_up_confirm_email_html").Replace("~", _
Request.PhysicalApplicationPath))
text.Replace("%%LINK%%", confirmUrl)
text.Replace("%%NAME%%", person.fname)
html.Replace("%%LINK%%", confirmUrl)
html.Replace("%%NAME%%", person.fname)
由于某些原因,我无法使%%LINK%%和%%NAME%%Replace()调用正常工作。我检查它是否与编码相关,所以我将每个文件都设置为UTF-8。还使用了ReadAllText(字符串,编码)的强制编码重载,但仍然没有骰子。有什么想法吗?问题是字符串在.NET中是不可变的。因此,您的替换代码应该如下所示:
text = text.Replace("%%LINK%%", confirmUrl);
text = text.Replace("%%NAME%%", person.fname);
html = html.Replace("%%LINK%%", confirmUrl);
html = html.Replace("%%NAME%%", person.fname);
许多人会说,让Replace()成为一个非静态函数对于.NET设计者来说是愚蠢的,所以不要觉得很糟糕;-)是的,我想每个.NET开发人员(如果我没记错的话,还有Java开发人员)都会遇到这种情况。甚至完全了解字符串不变性。见鬼,有时我还是忘了使用返回值。