VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > 简明python教程 >
  • C#tools(2)

String ToTxt(String Input) { StringBuilder sb = new StringBuilder(Input); sb.Replace("&nbsp;", " "); sb.Replace("<br>", "\r\n"); sb.Replace("<br>", "\n"); sb.Replace("<br />", "\n"); sb.Replace("<br />", "\r\n"); sb.Replace("&lt;", "<"); sb.Replace("&gt;", ">"); sb.Replace("&amp;", "&"); return sb.ToString(); } #endregion #region 检测是否有Sql危险字符 /// <summary> /// 检测是否有Sql危险字符 /// </summary> /// <param name="str">要判断字符串</param> /// <returns>判断结果</returns> public static bool IsSafeSqlString(string str) { return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']"); } /// <summary> /// 检查危险字符 /// </summary> /// <param name="Input"></param> /// <returns></returns> public static string Filter(string sInput) { if (sInput == null || sInput == "") return null; string sInput1 = sInput.ToLower(); string output = sInput; string pattern = @"*|and|exec|insert|select|delete|update|count|master|truncate|declare|char(|mid(|chr(|'"; if (Regex.Match(sInput1, Regex.Escape(pattern), RegexOptions.Compiled | RegexOptions.IgnoreCase).Success) { throw new Exception("字符串中含有非法字符!"); } else { output = output.Replace("'", "''"); } return output; } /// <summary> /// 检查过滤设定的危险字符 /// </summary> /// <param name="InText">要过滤的字符串 </param> /// <returns>如果参数存在不安全字符,则返回true </returns> public static bool SqlFilter(string word, string InText) { if (InText == null) return false; foreach (string i in word.Split('|')) { if ((InText.ToLower().IndexOf(i + " ") > -1) || (InText.ToLower().IndexOf(" " + i) > -1)) { return true; } } return false; } #endregion #region 过滤特殊字符 /// <summary> /// 过滤特殊字符 /// </summary> /// <param name="Input"></param> /// <returns></returns> public static string Htmls(string Input) { if (Input != string.Empty && Input != null) { string ihtml = Input.ToLower(); ihtml = ihtml.Replace("<script", "&lt;script"); ihtml = ihtml.Replace("script>", "script&gt;"); ihtml = ihtml.Replace("<%", "&lt;%"); ihtml = ihtml.Replace("%>", "%&gt;"); ihtml = ihtml.Replace("<$", "&lt;$"); ihtml = ihtml.Replace("$>", "$&gt;"); ihtml = ihtml.Replace("'", "&apos;"); ihtml = ihtml.Replace("\"", "&quot;"); ihtml = ihtml.Replace("<", "&lt;"); ihtml = ihtml = ihtml.Replace(">", "&gt;"); ihtml = ihtml.Replace("\r", "\\\r"); ihtml = ihtml.Replace("\n", "\\\n"); ihtml = ihtml.Replace("\t", " "); return ihtml; } else { return string.Empty; } } #endregion #region 检查是否为IP地址 /// <summary> /// 是否为ip /// </summary> /// <param name="ip"></param> /// <returns></returns> public static bool IsIP(string ip) { return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"); } #endregion #region 替换指定的字符串 /// <summary> /// 替换指定的字符串 /// </summary> /// <param name="originalStr">原字符串</param> /// <param name="oldStr">旧字符串</param> /// <param name="newStr">新字符串</param> /// <returns></returns> public static string ReplaceStr(string originalStr, string oldStr, string newStr) { if (string.IsNullOrEmpty(oldStr)) { return ""; } return originalStr.Replace(oldStr, newStr); } #endregion #region DATS转换城datetime /// <summary> /// 替换指定的DATS转换城datetime /// </summary> /// <param name="DatsStr">原字符串</param> /// <returns></returns> public static DateTime ReplaceDats(string datsStr) { DateTime datsTime = DateTime.MinValue; if (!string.IsNullOrEmpty(datsStr)) { if (datsStr.Trim().Length == 8) { string year = datsStr.Remove(4); string month = datsStr.Substring(4).Remove(2); string day = datsStr.Substring(4).Substring(2); datsTime = new DateTime(int.Parse(year), int.Parse(month), int.Parse(day)); } } return datsTime; } /// <summary> /// 获取枚举的特性 /// </summary> /// <param name="t"></param> /// <returns></returns> public static string DisplayEnum(Enum t) { var t_type = t.GetType(); var fieldName = Enum.GetName(t_type, t); var attributes = t_type.GetField(fieldName).GetCustomAttributes(false); var enumDisplayAttribute = attributes.FirstOrDefault(p => p.GetType().Equals(typeof(DisplayAttribute))) as DisplayAttribute; return enumDisplayAttribute == null ? fieldName : enumDisplayAttribute.Display; } /// <summary> /// 根据特性获取枚举 /// </summary> /// <param name="t"></param> /// <returns></returns> public static object DisplayEnum<T>(string code) { var t_type = typeof(T); var field = Enum.GetValues(typeof(T)).OfType<T>().ToList(); var attributes = t_type.GetFields().Select(x => new { attr = x.GetCustomAttributes(false).FirstOrDefault(y=>y.GetType().Equals(typeof(DisplayAttribute))) as DisplayAttribute, enu = x }).Where(x=>x.attr !=null); var enumValue = attributes.Where(x => x.attr.Display == code).FirstOrDefault(); if (enumValue != null) { return Enum.Parse(t_type, enumValue.enu.Name); } return null; } #endregion #region 获取GUID /// <summary> /// 获取32位的GUID /// </summary> /// <returns></returns> public static string GetGuid() { return Guid.NewGuid().ToString().Replace("-", "").ToUpper(); } /// <summary> /// 获取36位的GUID /// </summary> /// <returns></returns> public static string GetGuidLong() { return Guid.NewGuid().ToString().ToUpper(); } #endregion }
复制代码

相关教程