using Microsoft.AspNetCore.Http; using Microsoft.VisualBasic; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Data; using System.Linq; using System.Reflection; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Web; using System.Xml; namespace Ewide.NbzsZheliban.Tools { 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; } /// /// 过滤js脚本 /// /// /// public static string RemoveScript(this string html) { if (html.IsNullOrEmpty()) return string.Empty; System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" on[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"", System.Text.RegularExpressions.RegexOptions.IgnoreCase); html = regex1.Replace(html, ""); //过滤标记 html = regex2.Replace(html, ""); //过滤href=javascript: () 属性 html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件 html = regex4.Replace(html, ""); //过滤iframe html = regex5.Replace(html, ""); //过滤frameset return html; } /// /// 替换页面标签 /// /// /// public static string RemovePageTag(this string html) { if (html.IsNullOrEmpty()) return string.Empty; System.Text.RegularExpressions.Regex regex0 = new System.Text.RegularExpressions.Regex(@"]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@"", System.Text.RegularExpressions.RegexOptions.IgnoreCase); html = regex0.Replace(html, ""); //过滤标记 html = regex1.Replace(html, "标记 html = regex2.Replace(html, ""); //过滤属性 html = regex3.Replace(html, "属性 html = regex4.Replace(html, "属性 html = regex5.Replace(html, ""); //过滤属性 return html; } /// /// 取得html中的图片 /// /// /// public static string GetImg(this string text) { string str = string.Empty; Regex r = new Regex(@"]*\s*src\s*=\s*([']?)(?\S+)'?[^>]*>", //注意这里的(?\S+)是按正则表达式中的组来处理的,下面的代码中用使用到,也可以更改成其它的HTML标签,以同样的方法获得内容! RegexOptions.Compiled); Match m = r.Match(text.ToLower()); if (m.Success) str = m.Result("${url}").Replace("\"", "").Replace("'", ""); return str; } /// /// 取得html中的所有图片 /// /// /// public static string[] GetImgs(this string text) { List imgs = new List(); string pat = @"]*\s*src\s*=\s*([']?)(?\S+)'?[^>]*>"; Regex r = new Regex(pat, RegexOptions.Compiled); Match m = r.Match(text.ToLower()); while (m.Success) { imgs.Add(m.Result("${url}").Replace("\"", "").Replace("'", "")); m = m.NextMatch(); } return imgs.ToArray(); } /// /// 产生随机字符串 /// /// 字符串位数 public static string GetRandom(int length) { int number; char code; string checkCode = String.Empty; System.Random random = new Random(); for (int i = 0; i < length + 1; i++) { number = random.Next(); if (number % 2 == 0) code = (char)('0' + (char)(number % 10)); else code = (char)('A' + (char)(number % 26)); checkCode += code.ToString(); } return checkCode; } /// /// 字符串是否包含标点符号(不包括_下画线) /// /// /// public static bool InPunctuation(this string str) { foreach (char c in str.ToCharArray()) { if (char.IsPunctuation(c) && c != '_') return true; } return false; } /// /// 去除字符串标点符号和空字符 /// /// /// public static string RemovePunctuationOrEmpty(this string str) { StringBuilder NewString = new StringBuilder(str.Length); char[] charArr = str.ToCharArray(); foreach (char symbols in charArr) { if (!char.IsPunctuation(symbols) && !char.IsWhiteSpace(symbols)) { NewString.Append(symbols); } } return NewString.ToString(); } /// /// 返回带星期的日期格式 /// /// /// public static string ToDateWeekString(this DateTime date) { string week = string.Empty; switch (date.DayOfWeek) { case DayOfWeek.Friday: week = "五"; break; case DayOfWeek.Monday: week = "一"; break; case DayOfWeek.Saturday: week = "六"; break; case DayOfWeek.Sunday: week = "日"; break; case DayOfWeek.Thursday: week = "四"; break; case DayOfWeek.Tuesday: week = "二"; break; case DayOfWeek.Wednesday: week = "三"; break; } return date.ToString("yyyy年M月d日 ") + "星期" + week; } /// /// 返回带星期的日期时间格式 /// /// /// public static string ToDateTimeWeekString(this DateTime date) { string week = string.Empty; switch (date.DayOfWeek) { case DayOfWeek.Friday: week = "五"; break; case DayOfWeek.Monday: week = "一"; break; case DayOfWeek.Saturday: week = "六"; break; case DayOfWeek.Sunday: week = "日"; break; case DayOfWeek.Thursday: week = "四"; break; case DayOfWeek.Tuesday: week = "二"; break; case DayOfWeek.Wednesday: week = "三"; break; } return date.ToString("yyyy年M月d日H时m分") + " 星期" + week; } ///// ///// HTML编码 ///// ///// ///// //public static string HtmlEncode(this string str) //{ // return HttpContext.Current.Server.HtmlEncode(str); //} ///// ///// URL编码 ///// ///// ///// //public static string UrlEncode(this string str) //{ // return str.IsNullOrEmpty() ? string.Empty : HttpContext.Current.Server.UrlEncode(str); //} ///// ///// 获取与 Web 服务器上的指定虚拟路径相对应的物理文件路径。 ///// ///// ///// ///// //public static string MapPathExt(this HttpServerUtility Server, string path) //{ // if (path.StartsWith(@"\\") || path.Contains(":")) // { // return path; // } // else // { // return Server.MapPath(path); // } //} /// /// 使用CDATA的方式将value保存在指定的element中 /// /// /// public static void SetCDataValue(this System.Xml.Linq.XElement element, string value) { element.RemoveNodes(); element.Add(new System.Xml.Linq.XCData(value)); } /// /// 返回一个值,该值指示指定的 System.String 对象是否出现在此字符串中。 /// /// /// 要搜寻的字符串。 /// 指定搜索规则的枚举值之一 /// 如果 value 参数出现在此字符串中,或者 value 为空字符串 (""),则为 true;否则为 false public static bool Contains(this string source, string value, StringComparison comparisonType) { if (source == null || value == null) { return false; } if (value == "") { return true; } return (source.IndexOf(value, comparisonType) >= 0); } /// /// 通过使用默认的相等或字符比较器确定序列是否包含指定的元素。 /// /// 要在其中定位某个值的序列。 /// 要在序列中定位的值。 /// 指定搜索规则的枚举值之一 /// source 为 null /// 如果源序列包含具有指定值的元素,则为 true;否则为 false。 public static bool Contains(this string[] source, string value, StringComparison comparisonType) { return System.Linq.Enumerable.Contains(source, value, new CompareText(comparisonType)); } private class CompareText : IEqualityComparer { private StringComparison m_comparisonType { get; set; } public int GetHashCode(string t) { return t.GetHashCode(); } public CompareText(StringComparison comparisonType) { this.m_comparisonType = comparisonType; } public bool Equals(string x, string y) { if (x == y) { return true; } if (x == null || y == null) { return false; } else { return x.Equals(y, m_comparisonType); } } } /// /// 序列化对象为xml字符串 /// /// 要序列化的对象 /// xml格式字符串 public static string Serialize(this object obj) { if (obj == null) { return ""; } Type type = obj.GetType(); if (type.IsSerializable) { try { System.Text.StringBuilder sb = new System.Text.StringBuilder(); System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(type); XmlWriterSettings xset = new XmlWriterSettings(); xset.CloseOutput = true; xset.Encoding = Encoding.UTF8; xset.Indent = true; xset.CheckCharacters = false; System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(sb, xset); xs.Serialize(xw, obj); xw.Flush(); xw.Close(); return sb.ToString(); } catch { return ""; } } else { return ""; } } /// /// 将int类型转为GUID格式 /// /// /// public static Guid ToGuid(this int i) { return i.ToString("00000000-0000-0000-0000-000000000000").ToGuid(); } /// /// 将guid转换为int /// /// /// public static int ToInt(this Guid guid) { return guid.ToString("N").TrimStart('0').ToInt(); } public static string ToStringNoExp(this string str) { if (str == null) return string.Empty; return str; } public static string ToStringNoExp(this Guid str) { return str.ToString(); } public static string ToStringNoExp(this int str) { return str.ToString(); } public static string ToStringNoExp(this int? str) { if (str == null) return string.Empty; return str.ToString(); } public static string ToStringNoExp(this long str) { return str.ToString(); } public static string ToStringNoExp(this long? str) { if (str == null) return string.Empty; return str.ToString(); } public static string ToStringNoExp(this object str) { if (str == null) return string.Empty; return str.ToString(); } public static JObject ToJObject(this string str, bool isToLower = true) { return JObject.Parse(isToLower ? str.ToLower() : str); } public static JArray ToJArray(this string str, bool isToLower = true) { return JArray.Parse(isToLower ? str.ToLower() : str); } public static string GetJsonValue(this JObject jObject, string jsonName, bool isToLower = true, bool isThrowExp = false) { if (jObject == null) throw new Exception("参数为空"); JToken token = jObject[isToLower ? jsonName.ToLower() : jsonName]; if (token == null) { if (isThrowExp) throw new Exception(jsonName + "是必需的"); return string.Empty; } else return token.ToString(); } public static string GetJsonValue(this JArray jObject, string jsonName, bool isToLower = true) { JToken token = jObject[isToLower ? jsonName.ToLower() : jsonName]; if (token == null) return string.Empty; else return token.ToString(); } public static string GetJsonValue(this string param, string jsonName, bool isToLower = true, bool isThrowExp = false) { JObject jObject = param.ToJObject(isToLower); return GetJsonValue(jObject, jsonName, isToLower, isThrowExp); } public static DateTime? GetJsonDateTimeValue(this string jObjectstr, string jsonName, bool isToLower = true) { return jObjectstr.ToJObject(isToLower).GetJsonDateTimeValue(jsonName, isToLower); } public static DateTime? GetJsonDateTimeValue(this JObject jObject, string jsonName, bool isToLower = true) { string v = GetJsonValue(jObject, jsonName, isToLower); if (DateTime.TryParse(v, out DateTime dateTime)) { return dateTime; } else return null; } public static decimal? GetJsonDecimalValue(this string jObjectstr, string jsonName, bool isToLower = true) { return jObjectstr.ToJObject(isToLower).GetJsonDecimalValue(jsonName, isToLower); } public static decimal? GetJsonDecimalValue(this JObject jObject, string jsonName, bool isToLower = true) { string v = GetJsonValue(jObject, jsonName, isToLower); if (decimal.TryParse(v, out decimal d)) { return d; } else return null; } public static double? GetJsonDoubleValue(this string jObjectstr, string jsonName, bool isToLower = true) { return jObjectstr.ToJObject(isToLower).GetJsonDoubleValue(jsonName, isToLower); } public static double? GetJsonDoubleValue(this JObject jObject, string jsonName, bool isToLower = true) { string v = GetJsonValue(jObject, jsonName, isToLower); if (double.TryParse(v, out double d)) { return d; } else return null; } public static int? GetJsonIntValue(this string jObjectstr, string jsonName, bool isToLower = true) { return jObjectstr.ToJObject(isToLower).GetJsonIntValue(jsonName, isToLower); } public static int? GetJsonIntValue(this JObject jObject, string jsonName, bool isToLower = true, bool isThrowExp = false) { string v = GetJsonValue(jObject, jsonName, isToLower, isThrowExp); if (int.TryParse(v, out int i)) { return i; } else return null; } public static long? GetJsonLongValue(this string jObjectstr, string jsonName, bool isToLower = true, bool isThrowExp = false) { return jObjectstr.ToJObject(isToLower).GetJsonLongValue(jsonName, isToLower, isThrowExp); } public static long? GetJsonLongValue(this JObject jObject, string jsonName, bool isToLower = true, bool isThrowExp = false) { if (jObject == null) { if (isThrowExp) throw new Exception(jsonName + "是必需的"); return null; } string v = GetJsonValue(jObject, jsonName, isToLower); if (long.TryParse(v, out long d)) { return d; } else { if (isThrowExp) throw new Exception(jsonName + "是必需的"); return null; } } public static Guid? GetJsonGuidValue(this string jObjectstr, string jsonName, bool isToLower = true, bool isThrowExp = false) { return jObjectstr.ToJObject(isToLower).GetJsonGuidValue(jsonName, isToLower, isThrowExp); } public static Guid? GetJsonGuidValue(this JObject jObject, string jsonName, bool isToLower = true, bool isThrowExp = false) { string v = GetJsonValue(jObject, jsonName, isToLower); if (Guid.TryParse(v, out Guid d)) { return d; } else { if (isThrowExp) throw new Exception(jsonName + "是必需的"); return null; } } public static bool? GetJsonBoolValue(this JObject jObject, string jsonName, bool isToLower = true) { string v = jObject.GetJsonValue(jsonName, isToLower); if (bool.TryParse(v, out bool d)) { return d; } else return null; } public static bool? GetJsonBoolValue(this string jObjectstr, string jsonName, bool isToLower = true) { return jObjectstr.ToJObject(isToLower).GetJsonBoolValue(jsonName, isToLower); } /// /// /// /// /// /// public static string GetArrayValue(this JArray jarrat, int index) { JToken token = jarrat[index]; if (token == null) return string.Empty; else return token.ToString(); } public static JArray GetJsonJArrayValue(this string jObjectstr, string jsonName, bool isToLower = true) { return jObjectstr.ToJObject(isToLower).GetJsonJArrayValue(jsonName, isToLower); } public static JArray GetJsonJArrayValue(this JObject jObject, string jsonName, bool isToLower = true) { return jObject[isToLower ? jsonName.ToLower() : jsonName] as JArray; } public static JArray GetJsonJArray(this string param, string jsonName, bool isToLower = true) { var jObject = param.ToJObject(isToLower); return jObject[jsonName] as JArray; } /// /// /// /// /// json中参数名和where条件的键值对 /// public static string GetSqlQueryWhere(this string jsonstring, Dictionary dicWhere, bool isToLower = true) { if (string.IsNullOrEmpty(jsonstring)) return string.Empty; StringBuilder sqlWhere = new StringBuilder(); foreach (var item in dicWhere) { var itemkey = item.Key; var itemtype = string.Empty; if (itemkey.Contains(":")) { var itemkeys = itemkey.Split(':'); itemkey = itemkeys[0]; itemtype = itemkeys[1]; } var val = format(GetJsonValue(jsonstring, itemkey, isToLower), itemtype); if (!string.IsNullOrEmpty(val)) { sqlWhere.AppendFormat(item.Value, val); } } return sqlWhere.ToString(); } public static string GetSqlQueryWhereArray(this string jsonstring, Dictionary dicWhere, bool isToLower = true) { if (string.IsNullOrEmpty(jsonstring)) return string.Empty; StringBuilder sqlWhere = new StringBuilder(); foreach (var item in dicWhere) { var itemkey = item.Key; var itemtype = string.Empty; if (itemkey.Contains(":")) { var itemkeys = itemkey.Split(':'); itemkey = itemkeys[0]; itemtype = itemkeys[1]; } var val = format(GetJsonValue(jsonstring, itemkey, isToLower), itemtype); if (!string.IsNullOrEmpty(val)) { sqlWhere.AppendFormat(item.Value, val); } } return sqlWhere.ToString(); } private static string format(string v, string type) { switch (type) { case "bool": return string.IsNullOrEmpty(v) ? string.Empty : (new List { "是", "1" }.Contains(v.Trim()) ? "1" : "0"); case "date": case "datetime": return string.IsNullOrEmpty(v) ? string.Empty : Convert.ToDateTime(v).ToFormatString(); default: return v; } } /// /// json字符串转T /// /// /// /// public static T ToEntity(this JObject jobj, bool isToLower = true) where T : new() { var obj = new T(); PropertyInfo[] ProList = typeof(T).GetProperties(); foreach (PropertyInfo pro in ProList) { var propName = pro.PropertyType.Name.ToLower(); if ("nullable`1" == propName) { propName = pro.PropertyType.GetGenericArguments()[0].Name.ToLower(); } object propertyValue = pro.GetValue(obj, null); bool isnull = false; if (propertyValue == null) { isnull = true; } else { switch (propName) { case "guid": isnull = Guid.Parse(propertyValue.ToString()) == Guid.Empty; break; case "string": isnull = string.IsNullOrEmpty(propertyValue.ToString()); break; case "int": case "int32": case "int64": isnull = propertyValue.ToString().ToInt(0) == 0; break; } } if (isnull || true) { object value = null; switch (propName) { case "guid": value = jobj.GetJsonGuidValue(pro.Name, isToLower); break; case "string": value = jobj.GetJsonValue(pro.Name, isToLower); break; case "int": case "int32": value = jobj.GetJsonIntValue(pro.Name, isToLower); break; case "int64": value = jobj.GetJsonLongValue(pro.Name, isToLower); break; case "datetime": value = jobj.GetJsonDateTimeValue(pro.Name, isToLower); break; case "decimal": value = jobj.GetJsonDecimalValue(pro.Name, isToLower); break; case "list`1": value = jobj.GetJsonJArrayValue(pro.Name, isToLower); break; case "boolean": value = jobj.GetJsonBoolValue(pro.Name, isToLower); break; default: throw new Exception("补充类型:" + propName); } pro.SetValue(obj, value, null); } } return obj; //PropertyInfo[] ProList = typeof(T).GetProperties(); //foreach (PropertyInfo pro in ProList) //{ // try // { // if (page.Request.Form.AllKeys.Contains(pro.Name)) // { // string value = page.Request.Form[pro.Name]; // string pptypeName = pro.PropertyType.Name; // if (pptypeName == "Nullable`1") // { // if (pro.PropertyType.GetGenericArguments()[0] == typeof(Guid)) // { // pro.SetValue(obj, Guid.Parse(value), null); // } // else // { // pro.SetValue(obj, Convert.ChangeType(value, pro.PropertyType.GetGenericArguments()[0]), null); // } // } // else // { // pro.SetValue(obj, Convert.ChangeType(value, pro.PropertyType), null); // } // } // } // catch (Exception ee) // { // } //} //return obj; } /// /// json字符串转T /// /// /// /// public static T ToEntity(this string argsparam, bool isToLower = true) where T : new() { return argsparam.ToJObject(isToLower).ToEntity(); } /// /// 匹配符匹配查找字符串 /// /// /// /// /// public static string MatchSearch(this string input, string wildcardPattern, bool Greedy = false) { string regexPattern = wildcardPattern.Replace("?", ".").Replace("*", Greedy ? ".*?" : ".*"); System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(input, regexPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase); return m.Success ? m.Value : String.Empty; } public static List GetColumnValues(this DataTable dtSource, string fieledName) { return (from r in dtSource.AsEnumerable() select r.Field(fieledName)).ToList(); } public static string GetUtf8ByGbk(this string gbkstring) { return Encoding.UTF8.GetString(System.Text.Encoding.GetEncoding("GBK").GetBytes(gbkstring)); } public static string GetGbkByUtf8(this string utf8string) { return Encoding.GetEncoding("GBK").GetString(System.Text.Encoding.UTF8.GetBytes(utf8string)); } public static string GetGb2312ByUtf8(this string utf8string) { return Encoding.GetEncoding("gb2312").GetString(System.Text.Encoding.UTF8.GetBytes(utf8string)); } /// /// 获取url中的查询字符串参数 /// public static NameValueCollection GetQueryParamsByUrl(this string url) { int startIndex = url.IndexOf("?"); NameValueCollection values = new NameValueCollection(); if (startIndex <= 0) return values; string[] nameValues = url.Substring(startIndex + 1).Split('&'); foreach (string s in nameValues) { string[] pair = s.Split('='); string name = pair[0]; string value = string.Empty; if (pair.Length > 1) value = pair[1]; values.Add(name, value); } return values; } /// /// 获取url中的查询字符串参数 /// public static NameValueCollection GetQueryParamsByString(this string parameterString) { NameValueCollection values = new NameValueCollection(); string[] nameValues = parameterString.Split('&'); foreach (string s in nameValues) { string[] pair = s.Split('='); string name = pair[0]; string value = string.Empty; if (pair.Length > 1) value = pair[1]; values.Add(name, value); } return values; } private static DateTime Jan1st1900 = new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc); private static DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); private static DateTime Jan1st2000 = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc); private static DateTime Jan1st2019 = new DateTime(2019, 1, 1, 0, 0, 0, DateTimeKind.Utc); /// /// 获取自1970-1-01 00:00:00.000 到当前时刻的时间距离,类型为long ,值与 java中 System.currentTimeMillis() 相同 /// /// /// public static long currentTimeMillis(this DateTime d) { return (long)((d.ToUniversalTime() - Jan1st1970).TotalMilliseconds); } public static long getNewId(this DateTime d) { //return Convert.ToInt64(((d.ToUniversalTime() - Jan1st2000).TotalMilliseconds) + (BitConverter.ToUInt32(Guid.NewGuid().ToByteArray(), 0) % 100000).ToString("00000")); return Convert.ToInt64(((d.ToUniversalTime() - Jan1st2019).TotalMilliseconds).ToString("0") + (BitConverter.ToUInt32(Guid.NewGuid().ToByteArray(), 0) % 100000).ToString("00000")); //return (long)((d.ToUniversalTime() - Jan1st1970).TotalMilliseconds); } public static string ToFormatString(this DateTime d, string format = "yyyy-MM-dd HH:mm:ss") { return d.ToString(format); } /// /// 当前UTC时间戳,从2000年1月1日0点0分0秒开始到现在的秒数 /// /// /// public static long currentTimeSecond20000101(this DateTime d) { return (long)((d.ToUniversalTime() - Jan1st2000).TotalSeconds); } /// ///由秒数得到日期几天几小时几分几秒 ///天 ///0:转换后带秒,1:转换后不带秒 ///几天几小时几分几秒 public static string parseTimeSeconds(this float t, int type) { var zhengfu = t >= 0; t = t * 24 * 60 * 60;//转成小时秒 t = !zhengfu ? 0 - t : t; string r = ""; int day, hour, minute, second; if (t >= 86400) //天, { day = Convert.ToInt16(t / 86400); hour = Convert.ToInt16((t % 86400) / 3600); minute = Convert.ToInt16((t % 86400 % 3600) / 60); second = Convert.ToInt16(t % 86400 % 3600 % 60); if (type == 0) r = day + ("天") + hour + ("时") + minute + ("分") + second + ("秒"); else r = day + ("天") + hour + ("时") + minute + ("分"); } else if (t >= 3600)//时, { hour = Convert.ToInt16(t / 3600); minute = Convert.ToInt16((t % 3600) / 60); second = Convert.ToInt16(t % 3600 % 60); if (type == 0) r = hour + ("时") + minute + ("分") + second + ("秒"); else r = hour + ("时") + minute + ("分"); } else if (t >= 60)//分 { minute = Convert.ToInt16(t / 60); second = Convert.ToInt16(t % 60); r = minute + ("分") + second + ("秒"); } else { second = Convert.ToInt16(t); r = second + ("秒"); } return (zhengfu ? "" : "负") + r; } /// /// DateTime转换为时间戳 /// /// /// public static TimeSpan ToTimeSpan(this DateTime time) { return (time - DateTime.Now); } /// /// timeSpan转换为DateTime /// /// /// public static DateTime ToDateTime(this TimeSpan span) { return DateTime.Now.Add(span); } public static Int32 ToInt32(this object str) { Int32 test; Int32.TryParse(str.ToString(), out test); return test; } public static Int64 ToInt64(this object str) { Int64 test; Int64.TryParse(str.ToString(), out test); return test; } public static long ToLong(this object str) { return str.ToInt64(); } } }