在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using Newtonsoft.Json; using System.Web.Script.Serialization; using System.Reflection; using System.Security.Cryptography; namespace Utils { /// <summary> /// 字符串<see cref="String"/>类型的扩展辅助操作类 /// </summary> public static class ObjectHelper { #region 正则 /// <summary> /// 指示所指定的正则表达式在指定的输入字符串中是否找到了匹配项 /// </summary> /// <param name="value">要搜索匹配项的字符串</param> /// <param name="pattern">要匹配的正则表达式模式</param> /// <returns>如果正则表达式找到匹配项,则为 true;否则,为 false</returns> public static bool IsMatch(this string value, string pattern) { if (value == null) { return false; } return Regex.IsMatch(value, pattern); } /// <summary> /// 在指定的输入字符串中搜索指定的正则表达式的第一个匹配项 /// </summary> /// <param name="value">要搜索匹配项的字符串</param> /// <param name="pattern">要匹配的正则表达式模式</param> /// <returns>一个对象,包含有关匹配项的信息</returns> public static string Match(this string value, string pattern) { if (value == null) { return null; } return Regex.Match(value, pattern).Value; } /// <summary> /// 在指定的输入字符串中搜索指定的正则表达式的所有匹配项的字符串集合 /// </summary> /// <param name="value"> 要搜索匹配项的字符串 </param> /// <param name="pattern"> 要匹配的正则表达式模式 </param> /// <returns> 一个集合,包含有关匹配项的字符串值 </returns> public static IEnumerable<string> Matches(this string value, string pattern) { if (value == null) { return new string[] { }; } MatchCollection matches = Regex.Matches(value, pattern); return from Match match in matches select match.Value; } /// <summary> /// 是否电子邮件 /// </summary> public static bool IsEmail(this string value) { const string pattern = @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"; return value.IsMatch(pattern); } /// <summary> /// 是否是IP地址 /// </summary> public static bool IsIpAddress(this string value) { const string pattern = @"^(\d(25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\d\.){3}\d(25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\d$"; return value.IsMatch(pattern); } /// <summary> /// 是否是整数 /// </summary> public static bool IsNumeric(this string value) { const string pattern = @"^\-?[0-9]+$"; return value.IsMatch(pattern); } /// <summary> /// 是否是Unicode字符串 /// </summary> public static bool IsUnicode(this string value) { const string pattern = @"^[\u4E00-\u9FA5\uE815-\uFA29]+$"; return value.IsMatch(pattern); } /// <summary> /// 是否Url字符串 /// </summary> public static bool IsUrl(this string value) { const string pattern = @"^(http|https|ftp|rtsp|mms):(\/\/|\\\\)[A-Za-z0-9%\-_@]+\.[A-Za-z0-9%\-_@]+[A-Za-z0-9\.\/=\?%\-&_~`@:\+!;]*$"; return value.IsMatch(pattern); } /// <summary> /// 是否身份证号,验证如下3种情况: /// 1.身份证号码为15位数字; /// 2.身份证号码为18位数字; /// 3.身份证号码为17位数字+1个字母 /// </summary> public static bool IsIdentityCard(this string value) { const string pattern = @"^(^\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$"; return value.IsMatch(pattern); } /// <summary> /// 是否手机号码 /// </summary> /// <param name="value"></param> /// <param name="isRestrict">是否按严格格式验证</param> public static bool IsMobileNumber(this string value, bool isRestrict = false) { string pattern = isRestrict ? @"^[1][3-8]\d{9}$" : @"^[1]\d{10}$"; return value.IsMatch(pattern); } #endregion #region Json对象转换 /// <summary> /// 把对象序列化成Json字符串格式 /// </summary> /// <param name="object"></param> /// <returns></returns> public static string FromModelToJson(object @object) { // DoNet2.0 需要借助于Newtonsoft.Json.dll string json = JsonConvert.SerializeObject(@object); //// Donet3.5自带了DLL处理json串 //StringBuilder stringBuilder = new StringBuilder(); //JavaScriptSerializer jsonSerializer = new JavaScriptSerializer(); //jsonSerializer.Serialize(@object, stringBuilder); //json = stringBuilder.ToString(); return JsonDateTimeFormat(json); } /// <summary> /// 将JSON字符串还原为对象 /// </summary> /// <typeparam name="T">要转换的目标类型</typeparam> /// <param name="json">JSON字符串 </param> /// <returns></returns> public static T FromJsonToModel<T>(string json) { json = JsonDateTimeFormat(json); // DoNet2.0 需要借助于Newtonsoft.Json.dll JavaScriptSerializer Serializer = new JavaScriptSerializer(); return Serializer.Deserialize<T>(json); //// Donet3.5自带了DLL处理json串 //JavaScriptSerializer jsonSerializer = new JavaScriptSerializer(); //return jsonSerializer.Deserialize<T>(json); } /// <summary> /// 将JSON字符串还原为对象List /// </summary> /// <typeparam name="T"></typeparam> /// <param name="JsonStr"></param> /// <returns></returns> public static List<T> JSONStringToList<T>(string JsonStr) { JavaScriptSerializer Serializer = new JavaScriptSerializer(); //设置转化JSON格式时字段长度 List<T> objs = Serializer.Deserialize<List<T>>(JsonStr); return objs; } /// <summary> /// 处理Json的时间格式为正常格式 /// </summary> public static string JsonDateTimeFormat(string json) { json = Regex.Replace( json, @"\\/Date\((\d+)\)\\/", match => { DateTime dt = new DateTime(1970, 1, 1); dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value)); dt = dt.ToLocalTime(); return dt.ToString("yyyy-MM-dd HH:mm:ss.fff"); }); return json; } #endregion #region Byte字符串转换 /// <summary> /// 将字符串转换为<see cref="byte"/>[]数组,默认编码为<see cref="Encoding.UTF8"/> /// </summary> public static byte[] ToBytes(this string value, Encoding encoding = null) { if (encoding == null) { encoding = Encoding.UTF8; } return encoding.GetBytes(value); } /// <summary> /// 将<see cref="byte"/>[]数组转换为字符串,默认编码为<see cref="Encoding.UTF8"/> /// </summary> public static string ToString(this byte[] bytes, Encoding encoding = null) { if (encoding == null) { encoding = Encoding.UTF8; } return encoding.GetString(bytes); } #endregion #region 合并分割截取 /// <summary> /// 数组转字符串 /// </summary> /// <param name="values"></param> /// <param name="temp"></param> /// <returns></returns> public static string ArrayJoinToStr<T>(T[] values, string temp = ",") { return string.Join(temp, values); } /// <summary> /// 列表转字符串 /// </summary> /// <param name="values"></param> /// <param name="temp"></param> /// <returns></returns> public static string ListJoinToStr<T>(List<T> values, string temp = ",") { return string.Join(temp, values); } /// <summary> /// 以指定字符串作为分隔符将指定字符串分隔成数组 /// </summary> /// <param name="value">要分割的字符串</param> /// <param name="strSplit">字符串类型的分隔符</param> /// <param name="removeEmptyEntries">是否移除数据中元素为空字符串的项</param> /// <returns>分割后的数据</returns> public static string[] Split(this string value, string strSplit, bool removeEmptyEntries = false) { return value.Split(new[] { strSplit }, removeEmptyEntries ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None); } /// <summary> /// 保留开头指定数量的字符串,结尾替换为指定字符 /// </summary> /// <param name="str"></param> /// <param name="num"></param> /// <param name="temp"></param> /// <returns></returns> public static string GetStartStr(this string str, int num, string temp = "") { if (str != null) { if (str.Length > num) { str = str.Substring(0, num); if (!string.IsNullOrEmpty(temp)) return str + temp; } return str; } return ""; } /// <summary> /// 截取结尾指定数量的字符串,结尾为指定字符 /// </summary> /// <param name="str"></param> /// <param name="num"></param> /// <param name="temp"></param> /// <returns></returns> public static string ReplaceEndStr(this string str, int num, string temp = "") { if (str != null) { if (str.Length > num) { str = str.Substring(0, str.Length - num); if (!string.IsNullOrEmpty(temp)) return str + temp; } return str; } return ""; } /// <summary> /// 保留结尾指定数量的字符串,开头替换为指定字符 /// </summary> /// <param name="str"></param> /// <param name="num"></param> /// <param name="temp"></param> /// <returns></returns> public static string GetEndStr(this string str, int num, string temp = "") { string resStr = string.Empty; if (str != null) { if (str.Length > num) { str = str.Substring(str.Length - num); if (!string.IsNullOrEmpty(temp)) return temp + str; } return str; } return ""; } /// <summary> /// 截取开头指定数量的字符串,开头替换为指定字符 /// </summary> /// <param name="str"></param> /// <param name="num"></param> /// <param name="temp"></param> /// <returns></returns> public static string ReplaceStartStr(this string str, int num, string temp = "") { if (str != null) { if (str.Length > num) { str = str.Substring(num); if (!string.IsNullOrEmpty(temp)) return temp + str; } return str; } return ""; } /// <summary> /// 截取中间指定位置的字符,中间替换为指定字符 /// </summary> /// <param name="str"></param> /// <param name="num"></param> /// <param name="temp"></param> /// <returns></returns> public static string ReplaceMidStr(this string str, int startNum, int endNum, string temp = "") { if (str != null) { if (str.Length > startNum && str.Length > endNum && startNum < endNum) { if (!string.IsNullOrEmpty(temp)) { string s1 = str.Substring(0, startNum); string s2 = str.Substring(endNum); return s1 + temp + s2; } else return str.Remove(startNum, endNum - startNum); } return str; } return ""; } /// <summary> /// 截取中间指定长度的字符,中间替换为指定字符 /// </summary> /// <param name="str"></param> /// <param name="num"></param> /// <param name="temp"></param> /// <returns></returns> public static string ReplaceMidStrByLength(this string str, int startNum, int length, string temp = "") { if (str != null) { if (str.Length > startNum + length && length>0) { if (!string.IsNullOrEmpty(temp)) { string s1 = str.Substring(0, startNum); string s2 = str.Substring(length + length); return s1 + temp + s2; } else return str.Remove(startNum, length); } return str; } return ""; } #endregion #region 克隆对象 /// <summary> /// 克隆对象 /// </summary> /// <param name="o"></param> /// <returns></returns> public static object CloneObject(object o) { Type t = o.GetType(); PropertyInfo[] properties = t.GetProperties(); Object p = t.InvokeMember("", System.Reflection.BindingFlags.CreateInstance, null, o, null); foreach (PropertyInfo pi in properties) { if (pi.CanWrite) { object value = pi.GetValue(o, null); pi.SetValue(p, value, null); } } return p; } #endregion #region 唯一值 /// <summary> /// 获取GUID唯一字符串 /// (js和sql中也可生成唯一标识)http://www.ogeek.net/article/43823.htm /// </summary> /// <returns></returns> public static string GetGuid() { return Guid.NewGuid().ToString(); } /// <summary> /// 根据GUID获取16位的唯一字符串 /// </summary> /// <param name=\"guid\"></param> /// <returns></returns> public static string GuidTo16String() { long i = 1; foreach (byte b in Guid.NewGuid().ToByteArray()) i *= ((int)b + 1); return string.Format("{0:x}", i - DateTime.Now.Ticks); } /// <summary> /// 根据GUID获取唯一数字序列 /// </summary> /// <returns></returns> public static long GuidToLongID() { byte[] buffer = Guid.NewGuid().ToByteArray(); return BitConverter.ToInt64(buffer, 0); } /// <summary> /// 获取唯一字符串 /// 注:循环中连续取值是相同的 /// </summary> /// <returns></returns> public static string GetUniqueString() { Random rand = new Random(); return ((int)(rand.NextDouble() * 10000)).ToString() + DateTime.Now.Ticks.ToString(); } #endregion #region 字符过滤 /// <summary> /// 删除所有的html标记 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string ReplaceHtml(string str) { string[] Regexs = { @"<script[^>]*?>.*?</script>", @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>", @"([\r\n])[\s]+", @"&(quot|#34);", @"&(amp|#38);", @"&(lt|#60);", @"&(gt|#62);", @" |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论