欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

字符操作普通帮助类

程序员文章站 2022-04-15 10:49:43
/// /// 普通帮助类 /// public class CommonHelper { //星期数组 private static string[] _weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; //空格、回车、... ......
 /// <summary>
    /// 普通帮助类
    /// </summary>
    public class commonhelper
    {
        //星期数组
        private static string[] _weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        //空格、回车、换行符、制表符正则表达式
        private static regex _tbbrregex = new regex(@"\s*|\t|\r|\n", regexoptions.ignorecase);

        #region 时间操作

        /// <summary>
        /// 获得当前时间的""yyyy-mm-dd hh:mm:ss:fffffff""格式字符串
        /// </summary>
        public static string getdatetimems()
        {
            return datetime.now.tostring("yyyy-mm-dd hh:mm:ss:fffffff");
        }

        /// <summary>
        /// 获得当前时间的""yyyy年mm月dd日 hh:mm:ss""格式字符串
        /// </summary>
        public static string getdatetimeu()
        {
            return string.format("{0:u}", datetime.now);
        }

        /// <summary>
        /// 获得当前时间的""yyyy-mm-dd hh:mm:ss""格式字符串
        /// </summary>
        public static string getdatetime()
        {
            return datetime.now.tostring("yyyy-mm-dd hh:mm:ss");
        }

        /// <summary>
        /// 获得当前日期
        /// </summary>
        public static string getdate()
        {
            return datetime.now.tostring("yyyy-mm-dd");
        }

        /// <summary>
        /// 获得中文当前日期
        /// </summary>
        public static string getchinesedate()
        {
            return datetime.now.tostring("yyyy月mm日dd");
        }

        /// <summary>
        /// 获得当前时间(不含日期部分)
        /// </summary>
        public static string gettime()
        {
            return datetime.now.tostring("hh:mm:ss");
        }

        /// <summary>
        /// 获得当前小时
        /// </summary>
        public static string gethour()
        {
            return datetime.now.hour.tostring("00");
        }

        /// <summary>
        /// 获得当前天
        /// </summary>
        public static string getday()
        {
            return datetime.now.day.tostring("00");
        }

        /// <summary>
        /// 获得当前月
        /// </summary>
        public static string getmonth()
        {
            return datetime.now.month.tostring("00");
        }

        /// <summary>
        /// 获得当前年
        /// </summary>
        public static string getyear()
        {
            return datetime.now.year.tostring();
        }

        /// <summary>
        /// 获得当前星期(数字)
        /// </summary>
        public static string getdayofweek()
        {
            return ((int)datetime.now.dayofweek).tostring();
        }

        /// <summary>
        /// 获得当前星期(汉字)
        /// </summary>
        public static string getweek()
        {
            return _weekdays[(int)datetime.now.dayofweek];
        }

        #endregion

        #region 数组操作

        /// <summary>
        /// 获得字符串在字符串数组中的位置
        /// </summary>
        public static int getindexinarray(string s, string[] array, bool ignorecase)
        {
            if (string.isnullorempty(s) || array == null || array.length == 0)
                return -1;

            int index = 0;
            string temp = null;

            if (ignorecase)
                s = s.tolower();

            foreach (string item in array)
            {
                if (ignorecase)
                    temp = item.tolower();
                else
                    temp = item;

                if (s == temp)
                    return index;
                else
                    index++;
            }

            return -1;
        }

        /// <summary>
        /// 获得字符串在字符串数组中的位置
        /// </summary>
        public static int getindexinarray(string s, string[] array)
        {
            return getindexinarray(s, array, false);
        }

        /// <summary>
        /// 判断字符串是否在字符串数组中
        /// </summary>
        public static bool isinarray(string s, string[] array, bool ignorecase)
        {
            return getindexinarray(s, array, ignorecase) > -1;
        }

        /// <summary>
        /// 判断字符串是否在字符串数组中
        /// </summary>
        public static bool isinarray(string s, string[] array)
        {
            return isinarray(s, array, false);
        }

        /// <summary>
        /// 判断字符串是否在字符串中
        /// </summary>
        public static bool isinarray(string s, string array, string splitstr, bool ignorecase)
        {
            return isinarray(s, stringhelper.splitstring(array, splitstr), ignorecase);
        }

        /// <summary>
        /// 判断字符串是否在字符串中
        /// </summary>
        public static bool isinarray(string s, string array, string splitstr)
        {
            return isinarray(s, stringhelper.splitstring(array, splitstr), false);
        }

        /// <summary>
        /// 判断字符串是否在字符串中
        /// </summary>
        public static bool isinarray(string s, string array)
        {
            return isinarray(s, stringhelper.splitstring(array, ","), false);
        }



        /// <summary>
        /// 将对象数组拼接成字符串
        /// </summary>
        public static string objectarraytostring(object[] array, string splitstr)
        {
            if (array == null || array.length == 0)
                return "";

            stringbuilder result = new stringbuilder();
            for (int i = 0; i < array.length; i++)
                result.appendformat("{0}{1}", array[i], splitstr);

            return result.remove(result.length - splitstr.length, splitstr.length).tostring();
        }

        /// <summary>
        /// 将对象数组拼接成字符串
        /// </summary>
        public static string objectarraytostring(object[] array)
        {
            return objectarraytostring(array, ",");
        }

        /// <summary>
        /// 将字符串数组拼接成字符串
        /// </summary>
        public static string stringarraytostring(string[] array, string splitstr)
        {
            return objectarraytostring(array, splitstr);
        }

        /// <summary>
        /// 将字符串数组拼接成字符串
        /// </summary>
        public static string stringarraytostring(string[] array)
        {
            return stringarraytostring(array, ",");
        }

        /// <summary>
        /// 将整数数组拼接成字符串
        /// </summary>
        public static string intarraytostring(int[] array, string splitstr)
        {
            if (array == null || array.length == 0)
                return "";

            stringbuilder result = new stringbuilder();
            for (int i = 0; i < array.length; i++)
                result.appendformat("{0}{1}", array[i], splitstr);

            return result.remove(result.length - splitstr.length, splitstr.length).tostring();
        }

        /// <summary>
        /// 将整数数组拼接成字符串
        /// </summary>
        public static string intarraytostring(int[] array)
        {
            return intarraytostring(array, ",");
        }



        /// <summary>
        /// 移除数组中的指定项
        /// </summary>
        /// <param name="array">源数组</param>
        /// <param name="removeitem">要移除的项</param>
        /// <param name="removebackspace">是否移除空格</param>
        /// <param name="ignorecase">是否忽略大小写</param>
        /// <returns></returns>
        public static string[] removearrayitem(string[] array, string removeitem, bool removebackspace, bool ignorecase)
        {
            if (array != null && array.length > 0)
            {
                stringbuilder arraystr = new stringbuilder();
                if (ignorecase)
                    removeitem = removeitem.tolower();
                string temp = "";
                foreach (string item in array)
                {
                    if (ignorecase)
                        temp = item.tolower();
                    else
                        temp = item;

                    if (temp != removeitem)
                        arraystr.appendformat("{0}_", removebackspace ? item.trim() : item);
                }

                return stringhelper.splitstring(arraystr.remove(arraystr.length - 1, 1).tostring(), "_");
            }

            return array;
        }

        /// <summary>
        /// 移除数组中的指定项
        /// </summary>
        /// <param name="array">源数组</param>
        /// <returns></returns>
        public static string[] removearrayitem(string[] array)
        {
            return removearrayitem(array, "", true, false);
        }

        /// <summary>
        /// 移除字符串中的指定项
        /// </summary>
        /// <param name="s">源字符串</param>
        /// <param name="splitstr">分割字符串</param>
        /// <returns></returns>
        public static string[] removestringitem(string s, string splitstr)
        {
            return removearrayitem(stringhelper.splitstring(s, splitstr), "", true, false);
        }

        /// <summary>
        /// 移除字符串中的指定项
        /// </summary>
        /// <param name="s">源字符串</param>
        /// <returns></returns>
        public static string[] removestringitem(string s)
        {
            return removearrayitem(stringhelper.splitstring(s), "", true, false);
        }



        /// <summary>
        /// 移除数组中的重复项
        /// </summary>
        /// <returns></returns>
        public static int[] removerepeateritem(int[] array)
        {
            if (array == null || array.length < 2)
                return array;

            array.sort(array);

            int length = 1;
            for (int i = 1; i < array.length; i++)
            {
                if (array[i] != array[i - 1])
                    length++;
            }

            int[] uniquearray = new int[length];
            uniquearray[0] = array[0];
            int j = 1;
            for (int i = 1; i < array.length; i++)
                if (array[i] != array[i - 1])
                    uniquearray[j++] = array[i];

            return uniquearray;
        }

        /// <summary>
        /// 移除数组中的重复项
        /// </summary>
        /// <returns></returns>
        public static string[] removerepeateritem(string[] array)
        {
            if (array == null || array.length < 2)
                return array;

            array.sort(array);

            int length = 1;
            for (int i = 1; i < array.length; i++)
            {
                if (array[i] != array[i - 1])
                    length++;
            }

            string[] uniquearray = new string[length];
            uniquearray[0] = array[0];
            int j = 1;
            for (int i = 1; i < array.length; i++)
                if (array[i] != array[i - 1])
                    uniquearray[j++] = array[i];

            return uniquearray;
        }

        /// <summary>
        /// 去除字符串中的重复元素
        /// </summary>
        /// <returns></returns>
        public static string getuniquestring(string s)
        {
            return getuniquestring(s, ",");
        }

        /// <summary>
        /// 去除字符串中的重复元素
        /// </summary>
        /// <returns></returns>
        public static string getuniquestring(string s, string splitstr)
        {
            return objectarraytostring(removerepeateritem(stringhelper.splitstring(s, splitstr)), splitstr);
        }

        #endregion

        /// <summary>
        /// 去除字符串首尾处的空格、回车、换行符、制表符
        /// </summary>
        public static string tbbrtrim(string str)
        {
            if (!string.isnullorempty(str))
                return str.trim().trim('\r').trim('\n').trim('\t');
            return string.empty;
        }

        /// <summary>
        /// 去除字符串中的空格、回车、换行符、制表符
        /// </summary>
        public static string cleartbbr(string str)
        {
            if (!string.isnullorempty(str))
                return _tbbrregex.replace(str, "");

            return string.empty;
        }

        /// <summary>
        /// 删除字符串中的空行
        /// </summary>
        /// <returns></returns>
        public static string deletenullorspacerow(string s)
        {
            if (string.isnullorempty(s))
                return "";

            string[] temparray = stringhelper.splitstring(s, "\r\n");
            stringbuilder result = new stringbuilder();
            foreach (string item in temparray)
            {
                if (!string.isnullorwhitespace(item))
                    result.appendformat("{0}\r\n", item);
            }
            if (result.length > 0)
                result.remove(result.length - 2, 2);
            return result.tostring();
        }

        /// <summary>
        /// 获得指定数量的html空格
        /// </summary>
        /// <returns></returns>
        public static string gethtmlbs(int count)
        {
            if (count == 1)
                return "    ";
            else if (count == 2)
                return "        ";
            else if (count == 3)
                return "            ";
            else
            {
                stringbuilder result = new stringbuilder();

                for (int i = 0; i < count; i++)
                    result.append("    ");

                return result.tostring();
            }
        }

        /// <summary>
        /// 获得指定数量的htmlspan元素
        /// </summary>
        /// <returns></returns>
        public static string gethtmlspan(int count)
        {
            if (count <= 0)
                return "";

            if (count == 1)
                return "<span></span>";
            else if (count == 2)
                return "<span></span><span></span>";
            else if (count == 3)
                return "<span></span><span></span><span></span>";
            else
            {
                stringbuilder result = new stringbuilder();

                for (int i = 0; i < count; i++)
                    result.append("<span></span>");

                return result.tostring();
            }
        }

        /// <summary>
        ///获得邮箱提供者
        /// </summary>
        /// <param name="email">邮箱</param>
        /// <returns></returns>
        public static string getemailprovider(string email)
        {
            int index = email.lastindexof('@');
            if (index > 0)
                return email.substring(index + 1);
            return string.empty;
        }

        /// <summary>
        /// 转义正则表达式
        /// </summary>
        public static string escaperegex(string s)
        {
            string[] olist = { "\\", ".", "+", "*", "?", "{", "}", "[", "^", "]", "$", "(", ")", "=", "!", "<", ">", "|", ":" };
            string[] elist = { "\\\\", "\\.", "\\+", "\\*", "\\?", "\\{", "\\}", "\\[", "\\^", "\\]", "\\$", "\\(", "\\)", "\\=", "\\!", "\\<", "\\>", "\\|", "\\:" };
            for (int i = 0; i < olist.length; i++)
                s = s.replace(olist[i], elist[i]);
            return s;
        }

        /// <summary>
        /// 将ip地址转换成long类型
        /// </summary>
        /// <param name="ip">ip</param>
        /// <returns></returns>
        public static long convertiptolong(string ip)
        {
            string[] ips = ip.split('.');
            long number = 16777216l * long.parse(ips[0]) + 65536l * long.parse(ips[1]) + 256 * long.parse(ips[2]) + long.parse(ips[3]);
            return number;
        }

        /// <summary>
        /// 隐藏邮箱
        /// </summary>
        public static string hideemail(string email)
        {
            int index = email.lastindexof('@');

            if (index == 1)
                return "*" + email.substring(index);
            if (index == 2)
                return email[0] + "*" + email.substring(index);

            stringbuilder sb = new stringbuilder();
            sb.append(email.substring(0, 2));
            int count = index - 2;
            while (count > 0)
            {
                sb.append("*");
                count--;
            }
            sb.append(email.substring(index));
            return sb.tostring();
        }

        /// <summary>
        /// 隐藏手机
        /// </summary>
        public static string hidemobile(string mobile)
        {
            if (mobile != null && mobile.length > 10)
                return mobile.substring(0, 3) + "*****" + mobile.substring(8);
            return string.empty;
        }

        /// <summary>
        /// 数据转换为列表
        /// </summary>
        /// <param name="array">数组</param>
        /// <returns></returns>
        public static list<t> arraytolist<t>(t[] array)
        {
            list<t> list = new list<t>(array.length);
            foreach (t item in array)
                list.add(item);
            return list;
        }

        /// <summary> 
        /// datatable转化为list
        /// </summary> 
        /// <param name="dt">datatable</param> 
        /// <returns></returns> 
        public static list<dictionary<string, object>> datatabletolist(datatable dt)
        {
            int columncount = dt.columns.count;
            list<dictionary<string, object>> list = new list<dictionary<string, object>>(dt.rows.count);
            foreach (datarow dr in dt.rows)
            {
                dictionary<string, object> item = new dictionary<string, object>(columncount);
                for (int i = 0; i < columncount; i++)
                {
                    item.add(dt.columns[i].columnname, dr[i]);
                }
                list.add(item);
            }
            return list;
        }
    }