2021-05-17|2021-05-17 C# 过滤非法字符

region 过滤特殊字符

public static string SpecialCode(string s) { s = s.Replace(@"\", ""); s = Regex.Replace(s, "[ \\[ \\] \\^ \\-_*×――(^)$%~!/@#$…&%¥—+=<>《》|!!???::?`·、。,;,.; \"‘’“”-]", "").ToUpper(); return s; } #endregion

region 过滤非法字符 【2021-05-17|2021-05-17 C# 过滤非法字符】public static string encoding(string src)
{
if (src =https://www.it610.com/article/= null)
return "";
StringBuilder result = new StringBuilder();
if (src != null)
{
src = https://www.it610.com/article/src.Trim();
for (int pos = 0; pos < src.Length; pos++)
{
switch (src[pos])
{
case '"': result.Append("''"); break;
case '<': result.Append("<"); break;
case '>': result.Append(">"); break;
case ''': result.Append("'"); break;
case '&': result.Append("&"); break;
case '%': result.Append("&pc; "); break;
case '_': result.Append("&ul; "); break;
case '#': result.Append("&shap; "); break;
case '?': result.Append("&ques; "); break;
default: result.Append(src[pos]); break;
}
}
}
return result.ToString();
}
endregion

    推荐阅读