您好,登錄后才能下訂單哦!
這篇文章主要講解了“C#怎么對字符串剪裁、過濾危險字符、替換sql中有問題符號”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“C#怎么對字符串剪裁、過濾危險字符、替換sql中有問題符號”吧!
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace BIReportCenter.Utility { public class StringHelper { #region String length formatter /// <summary> /// 對字符串進行裁剪 /// </summary> public static string Trim(string stringTrim, int maxLength) { return Trim(stringTrim, maxLength, "..."); } /// <summary> /// 對字符串進行裁剪(區分單字節及雙字節字符) /// </summary> /// <param name="rawString">需要裁剪的字符串</param> /// <param name="maxLength">裁剪的長度,按雙字節計數</param> /// <param name="appendString">如果進行了裁剪需要附加的字符</param> public static string Trim(string rawString, int maxLength, string appendString) { if (string.IsNullOrEmpty(rawString) || rawString.Length <= maxLength) { return rawString; } else { int rawStringLength = Encoding.UTF8.GetBytes(rawString).Length; if (rawStringLength <= maxLength * 2) return rawString; } int appendStringLength = Encoding.UTF8.GetBytes(appendString).Length; StringBuilder checkedStringBuilder = new StringBuilder(); int appendedLenth = 0; for (int i = 0; i < rawString.Length; i++) { char _char = rawString[i]; checkedStringBuilder.Append(_char); appendedLenth += Encoding.Default.GetBytes(new char[] { _char }).Length; if (appendedLenth >= maxLength * 2 - appendStringLength) break; } return checkedStringBuilder.ToString() + appendString; } #endregion #region 特殊字符 /// <summary> /// 檢測是否有Sql危險字符 /// </summary> /// <param name="str">要判斷字符串</param> /// <returns>判斷結果</returns> public static bool IsSafeSqlString(string str) { return !Regex.IsMatch(str, @"[-|;|,|\/||| | |\}|\{|%|@|\*|!|\']"); } /// <summary> /// 刪除SQL注入特殊字符 /// 解然 20070622加入對輸入參數sql為Null的判斷 /// </summary> public static string StripSQLInjection(string sql) { if (!string.IsNullOrEmpty(sql)) { //過濾 ' -- string pattern1 = @"(\%27)|(\')|(\-\-)"; //防止執行 ' or string pattern2 = @"((\%27)|(\'))\s*((\%6F)|o|(\%4F))((\%72)|r|(\%52))"; //防止執行sql server 內部存儲過程或擴展存儲過程 string pattern3 = @"\s+exec(\s|\+)+(s|x)p\w+"; sql = Regex.Replace(sql, pattern1, string.Empty, RegexOptions.IgnoreCase); sql = Regex.Replace(sql, pattern2, string.Empty, RegexOptions.IgnoreCase); sql = Regex.Replace(sql, pattern3, string.Empty, RegexOptions.IgnoreCase); } return sql; } public static string SQLSafe(string Parameter) { Parameter = Parameter.ToLower(); Parameter = Parameter.Replace("'", ""); Parameter = Parameter.Replace(">", ">"); Parameter = Parameter.Replace("<", "<"); Parameter = Parameter.Replace("\n", "<br>"); Parameter = Parameter.Replace("\0", "·"); return Parameter; } /// <summary> /// 清除xml中的不合法字符 /// </summary> /// <remarks> /// 無效字符: /// 0x00 - 0x08 /// 0x0b - 0x0c /// 0x0e - 0x1f /// </remarks> public static string CleanInvalidCharsForXML(string input) { if (string.IsNullOrEmpty(input)) return input; else { StringBuilder checkedStringBuilder = new StringBuilder(); Char[] chars = input.ToCharArray(); for (int i = 0; i < chars.Length; i++) { int charValue = Convert.ToInt32(chars[i]); if ((charValue >= 0x00 && charValue <= 0x08) || (charValue >= 0x0b && charValue <= 0x0c) || (charValue >= 0x0e && charValue <= 0x1f)) continue; else checkedStringBuilder.Append(chars[i]); } return checkedStringBuilder.ToString(); //string result = checkedStringBuilder.ToString(); //result = result.Replace("?", ""); //return Regex.Replace(result, @"[\u0000-\u0008\u000B\u000C\u000E-\u001A\uD800-\uDFFF]", delegate(Match m) { int code = (int)m.Value.ToCharArray()[0]; return (code > 9 ? "&#" + code.ToString() : "?" + code.ToString()) + ";"; }); } } /// <summary> /// 改正sql語句中的轉義字符 /// </summary> public static string mashSQL(string str) { return (str == null) ? "" : str.Replace("\'", "'"); } /// <summary> /// 替換sql語句中的有問題符號 /// </summary> public static string ChkSQL(string str) { return (str == null) ? "" : str.Replace("'", "''"); } /// <summary> /// 判斷是否有非法字符 /// </summary> /// <param name="strString"></param> /// <returns>返回TRUE表示有非法字符,返回FALSE表示沒有非法字符。</returns> public static bool CheckBadStr(string strString) { bool outValue = false; if (strString != null && strString.Length > 0) { string[] bidStrlist = new string[9]; bidStrlist[0] = "'"; bidStrlist[1] = ";"; bidStrlist[2] = ":"; bidStrlist[3] = "%"; bidStrlist[4] = "@"; bidStrlist[5] = "&"; bidStrlist[6] = "#"; bidStrlist[7] = "\""; bidStrlist[8] = "net user"; bidStrlist[9] = "exec"; bidStrlist[10] = "net localgroup"; bidStrlist[11] = "select"; bidStrlist[12] = "asc"; bidStrlist[13] = "char"; bidStrlist[14] = "mid"; bidStrlist[15] = "insert"; bidStrlist[19] = "order"; bidStrlist[20] = "exec"; bidStrlist[21] = "delete"; bidStrlist[22] = "drop"; bidStrlist[23] = "truncate"; bidStrlist[24] = "xp_cmdshell"; bidStrlist[25] = "<"; bidStrlist[26] = ">"; string tempStr = strString.ToLower(); for (int i = 0; i < bidStrlist.Length; i++) { if (tempStr.IndexOf(bidStrlist[i]) != -1) //if (tempStr == bidStrlist[i]) { outValue = true; break; } } } return outValue; } #endregion #region Tools /// <summary> /// 去掉最后一個逗號 /// </summary> /// <param name="String">要做處理的字符串</param> /// <returns>去掉最后一個逗號的字符串</returns> public static string DelLastComma(string String) { if (String.IndexOf(",") == -1) { return String; } return String.Substring(0, String.LastIndexOf(",")); } /// <summary> /// 刪除最后一個字符 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string ClearLastChar(string str) { return (str == "") ? "" : str.Substring(0, str.Length - 1); } /// <summary> /// html編碼 /// </summary> /// <param name="chr"></param> /// <returns></returns> public static string html_text(string chr) { if (chr == null) return ""; chr = chr.Replace("'", "''"); chr = chr.Replace("<", "<"); chr = chr.Replace(">", ">"); return (chr); } /// <summary> /// html解碼 /// </summary> /// <param name="chr"></param> /// <returns></returns> public static string text_html(string chr) { if (chr == null) return ""; chr = chr.Replace("<", "<"); chr = chr.Replace(">", ">"); return (chr); } public static bool JustifyStr(string strValue) { bool flag = false; char[] str = "^<>'=&*, ".ToCharArray(0, 8); for (int i = 0; i < 8; i++) { if (strValue.IndexOf(str[i]) != -1) { flag = true; break; } } return flag; } public static string CheckOutputString(string key) { string OutputString = string.Empty; OutputString = key.Replace("<br>", "\n").Replace("<", "<").Replace(">", ">").Replace(" ", " "); return OutputString; } #endregion } }
感謝各位的閱讀,以上就是“C#怎么對字符串剪裁、過濾危險字符、替換sql中有問題符號”的內容了,經過本文的學習后,相信大家對C#怎么對字符串剪裁、過濾危險字符、替換sql中有問題符號這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。