• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#ObjectHelper

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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);",
                                    @"
                      

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#最大化和最小化启动窗体发布时间:2022-07-18
下一篇:
c#oracle存储过程分页查询发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap