问问题
960 次
2 回答
4
You are looking following code:
public string HTMLEncodeSpecialChars(string text)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (char c in text){
if(c>127) // chars not in ASCII
sb.Append(String.Format("&#{0};",(int)c));
else
sb.Append(c);
}
return sb.ToString();
}
于 2013-06-17T10:12:34.560 回答
1
If your input is in UTF, could you not just parse the text with a loop that does (pseudocode)
foreach char in string
if char code greater than 126
print "&#" + charcode + ";"
else if char is &
print "&"
else
print char
refinements possible...
It would be better to use the entity strings, IMO as they are clearer to human reader and less likely to be misinterpreted by computer. However, the above should work...
于 2013-06-17T10:11:19.840 回答