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

region 过滤特殊字符

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

region 过滤非法字符

public static string encoding(string src)
{
if (src == null)
return "";
StringBuilder result = new StringBuilder();
if (src != null)
{
src = 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

你可能感兴趣的:(2021-05-17 C# 过滤非法字符)