
/// <summary> /// Md5加密 /// </summary> /// <param name="strSource">待加密字串</param> /// <param name="length">16或32值之一,其它则采用.net默认MD5加密算法</param> /// <returns>加密后的字串</returns> public static string MD5Encrypt(string strSource, int length = 0) { byte[] bytes = Encoding.UTF8.GetBytes(strSource); byte[] hashValue = ((System.Security.Cryptography.HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(bytes); StringBuilder sb = new StringBuilder(); switch (length) { case 16: for (int i = 4; i < 12; i++) sb.Append(hashValue[i].ToString("x2")); break; case 32: for (int i = 0; i < 16; i++) { sb.Append(hashValue[i].ToString("x2")); } break; default: for (int i = 0; i < hashValue.Length; i++) { sb.Append(hashValue[i].ToString("x2")); } break; } return sb.ToString(); }

/// <summary> /// 隐藏手机号中间4位 /// </summary> /// <param name="mobile"></param> /// <returns></returns> public static string ParserMobile(string mobile) { if (string.IsNullOrEmpty(mobile)) return string.Empty; if (mobile.Length != 11) return mobile; var start = mobile.Substring(0, 3); var end = mobile.Substring(mobile.Length - 4, 4); return string.Format("{0}****{1}", start, end); }

/// <summary> /// 格式化QueryString /// </summary> /// <param name="queryString"></param> /// <returns></returns> public static NameValueCollection PaserToKeyValue(string queryString) { try { var coll = System.Web.HttpUtility.ParseQueryString(queryString); return coll; } catch { } return null; }

/// <summary> /// 字符串转Int /// </summary> /// <param name="val"></param> /// <returns></returns> public static int ToInt(string val) { int i; int.TryParse(val,out i); return i; }

/// <summary> /// 字符串转Int64 /// </summary> /// <param name="val"></param> /// <returns></returns> public static long ToInt64(string val) { long v; if (!long.TryParse(val, out v)) { v = 0; } return v; }

/// <summary> /// 字符串转Int? /// </summary> /// <param name="val"></param> /// <returns></returns> public static int? ToNullInt(string val) { int? i; try { i = Convert.ToInt32(val); } catch { i = null; } return i; }

/// <summary> /// HtmlToStr /// </summary> /// <param name="htmlstring"></param> /// <param name="replaceto"></param> /// <returns></returns> public static string HtmlToString(string htmlstring, string replaceto = " ") { if (string.IsNullOrEmpty(htmlstring)) return string.Empty; //删除脚本 htmlstring = Regex.Replace(htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase); //删除HTML htmlstring = Regex.Replace(htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase); htmlstring = Regex.Replace(htmlstring, @"([ ])[s]+", "", RegexOptions.IgnoreCase); htmlstring = Regex.Replace(htmlstring, @"-->", "", RegexOptions.IgnoreCase); htmlstring = Regex.Replace(htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase); htmlstring = Regex.Replace(htmlstring, @"&(quot|#34);", """, RegexOptions.IgnoreCase); htmlstring = Regex.Replace(htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase); htmlstring = Regex.Replace(htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase); htmlstring = Regex.Replace(htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase); htmlstring = Regex.Replace(htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase); htmlstring = Regex.Replace(htmlstring, @"&(iexcl|#161);", "xa1", RegexOptions.IgnoreCase); htmlstring = Regex.Replace(htmlstring, @"&(cent|#162);", "xa2", RegexOptions.IgnoreCase); htmlstring = Regex.Replace(htmlstring, @"&(pound|#163);", "xa3", RegexOptions.IgnoreCase); htmlstring = Regex.Replace(htmlstring, @"&(copy|#169);", "xa9", RegexOptions.IgnoreCase); htmlstring = Regex.Replace(htmlstring, @"&#(d+);", "", RegexOptions.IgnoreCase); htmlstring = htmlstring.Replace("<", ""); htmlstring = htmlstring.Replace(">", ""); htmlstring = htmlstring.Replace(" ", ""); return htmlstring; }

/// <summary> /// 替换字符 /// </summary> /// <param name="str"></param> /// <param name="length"></param> /// <param name="endstr"></param> /// <returns></returns> public static string Replace(string str, int length, string endstr = "...") { if (!string.IsNullOrEmpty(str)) { if (str.Length > length) { str = str.Substring(0, length) + endstr; } } return str; }

/// <summary> /// 查询Config值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> public static T GetAppSetting<T>(string key) { try { var val = ConfigurationManager.AppSettings[key]; return StringHelper.ChangeType<T>(val); } catch { return default(T); } }

/// <summary> /// 更改类型 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="v"></param> /// <returns></returns> public static T ChangeType<T>(object v) { try { return (T)Convert.ChangeType(v, typeof(T)); } catch { return default(T); } }