using System; using System.Web; using System.Text; using System.Text.RegularExpressions; using System.Xml; using System.Collections.Generic; using Microsoft.VisualBasic; /// ///ExtString 的摘要说明 /// public static class MyExtensions { public static bool IsDecimal(this string str) { decimal test; return decimal.TryParse(str, out test); } public static bool IsDecimal(this string str, out decimal test) { return decimal.TryParse(str, out test); } public static bool IsDouble(this string str) { double test; return double.TryParse(str, out test); } public static bool IsDouble(this string str, out double test) { return double.TryParse(str, out test); } /// /// 格式化数字,三位加逗号 /// /// /// public static string ToFormatString(this decimal str) { string str1 = str.ToString(); return str1.IndexOf('.') >= 0 ? str.ToString("#,##0" + ".".PadRight(str1.Substring(str1.IndexOf('.')).Length, '0')) : str.ToString("#,##0"); } /// /// 相加 /// /// /// /// public static double Add(this double d1, double d2) { return (double)((decimal)d1 + (decimal)d2); } /// /// 相减 /// /// /// /// public static double sub(this double d1, double d2) { return (double)((decimal)d1 - (decimal)d2); } /// /// 相乖 /// /// /// /// public static double mul(this double d1, double d2) { return (double)((decimal)d1 * (decimal)d2); } /// /// 相除 /// /// /// /// public static double div(this double d1, double d2) { return d2 == 0 ? 0 : (double)((decimal)d1 / (decimal)d2); } public static bool IsInt(this string str) { int test; return int.TryParse(str, out test); } public static bool IsInt(this string str, out int test) { return int.TryParse(str, out test); } /// /// 将数组转换为符号分隔的字符串 /// /// /// 分隔符 /// public static string Join1(this T[] arr, string split = ",") { StringBuilder sb = new StringBuilder(arr.Length * 36); for (int i = 0; i < arr.Length; i++) { sb.Append(arr[i].ToString()); if (i < arr.Length - 1) { sb.Append(split); } } return sb.ToString(); } /// /// 去除所有空格 /// /// /// public static string RemoveSpace(this string str) { if (str.IsNullOrEmpty()) return ""; return str.Replace("", " ").Replace("\r", "").Replace("\n", ""); } public static bool IsLong(this string str) { long test; return long.TryParse(str, out test); } public static bool IsLong(this string str, out long test) { return long.TryParse(str, out test); } public static bool IsDateTime(this string str) { DateTime test; return DateTime.TryParse(str, out test); } public static bool IsDateTime(this string str, out DateTime test) { return DateTime.TryParse(str, out test); } public static bool IsGuid(this string str) { Guid test; return Guid.TryParse(str, out test); } public static bool IsGuid(this string str, out Guid test) { return Guid.TryParse(str, out test); } /// /// 判断是否为Guid.Empty /// /// /// public static bool IsEmptyGuid(this Guid guid) { return guid == Guid.Empty; } public static bool IsUrl(this string str) { if (str.IsNullOrEmpty()) return false; string pattern = @"^(http|https|ftp|rtsp|mms):(\/\/|\\\\)[A-Za-z0-9%\-_@]+\.[A-Za-z0-9%\-_@]+[A-Za-z0-9\.\/=\?%\-&_~`@:\+!;]*$"; return Regex.IsMatch(str, pattern, RegexOptions.IgnoreCase); } public static bool IsEmail(this string str) { return Regex.IsMatch(str, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); } /// /// 判断一个整型是否包含在指定的值内 /// /// /// public static bool In(this int i, params int[] ints) { foreach (int k in ints) { if (i == k) { return true; } } return false; } /// /// 返回值或DBNull.Value /// /// /// public static object DBValueOrNull(this string str) { if (str.IsNullOrEmpty()) { return null; } else { return str; } } public static decimal ToDecimal(this string str, int decimals) { decimal test; return decimal.TryParse(str, out test) ? decimal.Round(test, decimals, MidpointRounding.AwayFromZero) : 0; } public static decimal ToDecimal(this string str) { decimal test; return decimal.TryParse(str, out test) ? test : 0; } public static decimal Round(this decimal dec, int decimals = 2) { return Math.Round(dec, decimals, MidpointRounding.AwayFromZero); } public static double ToDouble(this string str, int digits) { double test; return double.TryParse(str, out test) ? test.Round(digits) : 0; } public static double ToDouble(this string str) { double test; return double.TryParse(str, out test) ? test : 0; } public static double Round(this double value, int decimals) { if (value < 0) return Math.Round(value + 5 / Math.Pow(10, decimals + 1), decimals, MidpointRounding.AwayFromZero); else return Math.Round(value, decimals, MidpointRounding.AwayFromZero); } public static short ToShort(this string str) { short test; short.TryParse(str, out test); return test; } public static int? ToIntOrNull(this string str) { int test; if (int.TryParse(str, out test)) { return test; } else { return null; } } public static int ToInt(this string str) { int test; int.TryParse(str, out test); return test; } public static int ToInt(this string str, int defaultValue) { int test; return int.TryParse(str, out test) ? test : defaultValue; } public static long ToLong(this string str) { long test; long.TryParse(str, out test); return test; } public static Int16 ToInt16(this string str) { Int16 test; Int16.TryParse(str, out test); return test; } public static Int32 ToInt32(this string str) { Int32 test; Int32.TryParse(str, out test); return test; } public static Int64 ToInt64(this string str) { Int64 test; Int64.TryParse(str, out test); return test; } public static DateTime ToDateTime(this string str) { DateTime test; DateTime.TryParse(str, out test); return test; } public static DateTime? ToDateTimeOrNull(this string str) { DateTime test; if (DateTime.TryParse(str, out test)) { return test; } return null; } public static Guid ToGuid(this string str) { Guid test; if (Guid.TryParse(str, out test)) { return test; } else { return Guid.Empty; } } /// /// 尝试转换为Boolean类型 /// /// /// public static bool ToBoolean(this string str) { bool b; return Boolean.TryParse(str, out b) ? b : false; } /// /// 尝试格式化日期字符串 /// /// /// /// public static string DateFormat(this object date, string format = "yyyy/MM/dd") { if (date == null) { return string.Empty; } DateTime d; if (!date.ToString().IsDateTime(out d)) { return date.ToString(); } else { return d.ToString(format); } } public static bool IsNullOrEmpty(this string str) { return string.IsNullOrWhiteSpace(str); } public static string ToString(this IList strList, char split) { return strList.ToString(split.ToString()); } public static string ToString(this IList strList, string split) { StringBuilder sb = new StringBuilder(strList.Count * 10); for (int i = 0; i < strList.Count; i++) { sb.Append(strList[i]); if (i < strList.Count - 1) { sb.Append(split); } } return sb.ToString(); } /// /// 过滤sql /// /// /// public static string ReplaceSql(this string str) { str = str.Replace("'", "").Replace("--", " ").Replace(";", ""); return str; } /// /// 过滤查询sql /// /// /// public static string ReplaceSelectSql(this string str) { if (str.IsNullOrEmpty()) return ""; str = str.Replace1("DELETE ", "").Replace1("UPDATE ", "").Replace1("INSERT ", ""); return str; } /// /// 过滤字符串(不区分大小写) /// /// /// public static string Replace1(this string str, string oldString, string newString) { return str.IsNullOrEmpty() ? "" : Strings.Replace(str, oldString, newString, 1, -1, CompareMethod.Text); } /// /// 获取汉字拼音的第一个字母 /// /// /// public static string ToChineseSpell(this string strText) { int len = strText.Length; string myStr = ""; for (int i = 0; i < len; i++) { myStr += getSpell(strText.Substring(i, 1)); } return myStr.ToLower(); } /// /// 获取汉字拼音 /// /// /// public static string getSpell(this string cnChar) { byte[] arrCN = Encoding.Default.GetBytes(cnChar); if (arrCN.Length > 1) { int area = (short)arrCN[0]; int pos = (short)arrCN[1]; int code = (area << 8) + pos; int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 }; for (int i = 0; i < 26; i++) { int max = 55290; if (i != 25) max = areacode[i + 1]; if (areacode[i] <= code && code < max) { return Encoding.Default.GetString(new byte[] { (byte)(65 + i) }); } } return "x"; } else return cnChar; } /// /// 截取字符串,汉字两个字节,字母一个字节 /// /// 字符串 /// 字符串长度 /// public static string Interruption(this string str, int len, string show) { ASCIIEncoding ascii = new ASCIIEncoding(); int tempLen = 0; string tempString = ""; byte[] s = ascii.GetBytes(str); for (int i = 0; i < s.Length; i++) { if ((int)s[i] == 63) { tempLen += 2; } else { tempLen += 1; } try { tempString += str.Substring(i, 1); } catch { break; } if (tempLen > len) break; } //如果截过则加上半个省略号 byte[] mybyte = System.Text.Encoding.Default.GetBytes(str); if (mybyte.Length > len) tempString += show; tempString = tempString.Replace(" ", " "); tempString = tempString.Replace("<", "<"); tempString = tempString.Replace(">", ">"); tempString = tempString.Replace('\n'.ToString(), "
"); return tempString; } /// /// 截取字符串,汉字两个字节,字母一个字节 /// /// 字符串 /// 字符串长度 /// public static string CutString(this string str, int len, string show = "...") { return Interruption(str, len, show); } /// /// 获取左边多少个字符 /// /// /// /// public static string Left(this string str, int len) { if (str == null || len < 1) { return ""; } if (len < str.Length) { return str.Substring(0, len); } else { return str; } } /// /// 获取右边多少个字符 /// /// /// /// public static string Right(this string str, int len) { if (str == null || len < 1) { return ""; } if (len < str.Length) { return str.Substring(str.Length - len); } else { return str; } } /// /// 得到实符串实际长度 /// /// /// public static int Size(this string str) { byte[] strArray = System.Text.Encoding.Default.GetBytes(str); int res = strArray.Length; return res; } /// /// 去除HTML标记 /// /// 包括HTML的源码 /// 已经去除后的文字 public static string RemoveHTML(this string Htmlstring) { //删除脚本 Htmlstring = Regex.Replace(Htmlstring, @"]*?>.*?", "", RegexOptions.IgnoreCase); //删除HTML Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"