考虑我有以下字符串:
string s = "hello a & b, <hello world >"
我想用"&"
(b / wa和b)替换"&"
所以,如果我用
s.replace("&", "&");
它还将替换与<
和关联的“&” >
。
有什么办法可以在a和b之间仅替换“&”?
您可以使用HttpUtility.HtmlEncode
&HttpUtility.HtmlDecode
如下所示。
首先对您的字符串进行解码以获取普通字符串,然后再次对其进行编码,这将为您提供预期的字符串。
HttpUtility.HtmlEncode(HttpUtility.HtmlDecode("hello a & b, <hello world >"));
HttpUtility.HtmlDecode("hello a & b, <hello world >")
会回来的hello a & b, <hello world >
。
HttpUtility.HtmlEncode("hello a & b, <hello world >")
将返回 hello a & b, <hello world >
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句