0

0

字符如何操作普通帮助类?字符操作普通帮助类的方法(代码示例)

青灯夜游

青灯夜游

发布时间:2018-10-24 13:46:47

|

2623人浏览过

|

来源于博客园

转载

本篇文章给大家带来的内容是介绍字符如何操作普通帮助类?字符操作普通帮助类的方法(代码示例)。有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助。

 /// 
    /// 普通帮助类
    /// 
    public class CommonHelper
    {
        //星期数组
        private static string[] _weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        //空格、回车、换行符、制表符正则表达式
        private static Regex _tbbrRegex = new Regex(@"\s*|\t|\r|\n", RegexOptions.IgnoreCase);

        #region 时间操作

        /// 
        /// 获得当前时间的""yyyy-MM-dd HH:mm:ss:fffffff""格式字符串
        /// 
        public static string GetDateTimeMS()
        {
            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fffffff");
        }

        /// 
        /// 获得当前时间的""yyyy年MM月dd日 HH:mm:ss""格式字符串
        /// 
        public static string GetDateTimeU()
        {
            return string.Format("{0:U}", DateTime.Now);
        }

        /// 
        /// 获得当前时间的""yyyy-MM-dd HH:mm:ss""格式字符串
        /// 
        public static string GetDateTime()
        {
            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        /// 
        /// 获得当前日期
        /// 
        public static string GetDate()
        {
            return DateTime.Now.ToString("yyyy-MM-dd");
        }

        /// 
        /// 获得中文当前日期
        /// 
        public static string GetChineseDate()
        {
            return DateTime.Now.ToString("yyyy月MM日dd");
        }

        /// 
        /// 获得当前时间(不含日期部分)
        /// 
        public static string GetTime()
        {
            return DateTime.Now.ToString("HH:mm:ss");
        }

        /// 
        /// 获得当前小时
        /// 
        public static string GetHour()
        {
            return DateTime.Now.Hour.ToString("00");
        }

        /// 
        /// 获得当前天
        /// 
        public static string GetDay()
        {
            return DateTime.Now.Day.ToString("00");
        }

        /// 
        /// 获得当前月
        /// 
        public static string GetMonth()
        {
            return DateTime.Now.Month.ToString("00");
        }

        /// 
        /// 获得当前年
        /// 
        public static string GetYear()
        {
            return DateTime.Now.Year.ToString();
        }

        /// 
        /// 获得当前星期(数字)
        /// 
        public static string GetDayOfWeek()
        {
            return ((int)DateTime.Now.DayOfWeek).ToString();
        }

        /// 
        /// 获得当前星期(汉字)
        /// 
        public static string GetWeek()
        {
            return _weekdays[(int)DateTime.Now.DayOfWeek];
        }

        #endregion

        #region 数组操作

        /// 
        /// 获得字符串在字符串数组中的位置
        /// 
        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;
        }

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

        /// 
        /// 判断字符串是否在字符串数组中
        /// 
        public static bool IsInArray(string s, string[] array, bool ignoreCase)
        {
            return GetIndexInArray(s, array, ignoreCase) > -1;
        }

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

        /// 
        /// 判断字符串是否在字符串中
        /// 
        public static bool IsInArray(string s, string array, string splitStr, bool ignoreCase)
        {
            return IsInArray(s, StringHelper.SplitString(array, splitStr), ignoreCase);
        }

        /// 
        /// 判断字符串是否在字符串中
        /// 
        public static bool IsInArray(string s, string array, string splitStr)
        {
            return IsInArray(s, StringHelper.SplitString(array, splitStr), false);
        }

        /// 
        /// 判断字符串是否在字符串中
        /// 
        public static bool IsInArray(string s, string array)
        {
            return IsInArray(s, StringHelper.SplitString(array, ","), false);
        }



        /// 
        /// 将对象数组拼接成字符串
        /// 
        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();
        }

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

        /// 
        /// 将字符串数组拼接成字符串
        /// 
        public static string StringArrayToString(string[] array, string splitStr)
        {
            return ObjectArrayToString(array, splitStr);
        }

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

        /// 
        /// 将整数数组拼接成字符串
        /// 
        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();
        }

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



        /// 
        /// 移除数组中的指定项
        /// 
        /// 源数组
        /// 要移除的项
        /// 是否移除空格
        /// 是否忽略大小写
        /// 
        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;
        }

        /// 
        /// 移除数组中的指定项
        /// 
        /// 源数组
        /// 
        public static string[] RemoveArrayItem(string[] array)
        {
            return RemoveArrayItem(array, "", true, false);
        }

        /// 
        /// 移除字符串中的指定项
        /// 
        /// 源字符串
        /// 分割字符串
        /// 
        public static string[] RemoveStringItem(string s, string splitStr)
        {
            return RemoveArrayItem(StringHelper.SplitString(s, splitStr), "", true, false);
        }

        /// 
        /// 移除字符串中的指定项
        /// 
        /// 源字符串
        /// 
        public static string[] RemoveStringItem(string s)
        {
            return RemoveArrayItem(StringHelper.SplitString(s), "", true, false);
        }



        /// 
        /// 移除数组中的重复项
        /// 
        /// 
        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;
        }

        /// 
        /// 移除数组中的重复项
        /// 
        /// 
        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;
        }

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

        /// 
        /// 去除字符串中的重复元素
        /// 
        /// 
        public static string GetUniqueString(string s, string splitStr)
        {
            return ObjectArrayToString(RemoveRepeaterItem(StringHelper.SplitString(s, splitStr)), splitStr);
        }

        #endregion

        /// 
        /// 去除字符串首尾处的空格、回车、换行符、制表符
        /// 
        public static string TBBRTrim(string str)
        {
            if (!string.IsNullOrEmpty(str))
                return str.Trim().Trim('\r').Trim('\n').Trim('\t');
            return string.Empty;
        }

        /// 
        /// 去除字符串中的空格、回车、换行符、制表符
        /// 
        public static string ClearTBBR(string str)
        {
            if (!string.IsNullOrEmpty(str))
                return _tbbrRegex.Replace(str, "");

            return string.Empty;
        }

        /// 
        /// 删除字符串中的空行
        /// 
        /// 
        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();
        }

        /// 
        /// 获得指定数量的html空格
        /// 
        /// 
        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();
            }
        }

        /// 
        /// 获得指定数量的htmlSpan元素
        /// 
        /// 
        public static string GetHtmlSpan(int count)
        {
            if (count <= 0)
                return "";

            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();
            }
        }

        /// 
        ///获得邮箱提供者
        /// 
        /// 邮箱
        /// 
        public static string GetEmailProvider(string email)
        {
            int index = email.LastIndexOf('@');
            if (index > 0)
                return email.Substring(index + 1);
            return string.Empty;
        }

        /// 
        /// 转义正则表达式
        /// 
        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;
        }

        /// 
        /// 将ip地址转换成long类型
        /// 
        /// ip
        /// 
        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;
        }

        /// 
        /// 隐藏邮箱
        /// 
        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();
        }

        /// 
        /// 隐藏手机
        /// 
        public static string HideMobile(string mobile)
        {
            if (mobile != null && mobile.Length > 10)
                return mobile.Substring(0, 3) + "*****" + mobile.Substring(8);
            return string.Empty;
        }

        /// 
        /// 数据转换为列表
        /// 
        /// 数组
        /// 
        public static List ArrayToList(T[] array)
        {
            List list = new List(array.Length);
            foreach (T item in array)
                list.Add(item);
            return list;
        }

        ///  
        /// DataTable转化为List
        ///  
        /// DataTable 
        ///  
        public static List> DataTableToList(DataTable dt)
        {
            int columnCount = dt.Columns.Count;
            List> list = new List>(dt.Rows.Count);
            foreach (DataRow dr in dt.Rows)
            {
                Dictionary item = new Dictionary(columnCount);
                for (int i = 0; i < columnCount; i++)
                {
                    item.Add(dt.Columns[i].ColumnName, dr[i]);
                }
                list.Add(item);
            }
            return list;
        }
    }

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。更多相关视频教程可以访问:ASP.NET视频教程

PaperAiBye
PaperAiBye

支持近30多种语言降ai降重,并且支持多种语言免费测句子的ai率,支持英文aigc报告等

下载

相关专题

更多
高德地图升级方法汇总
高德地图升级方法汇总

本专题整合了高德地图升级相关教程,阅读专题下面的文章了解更多详细内容。

4

2026.01.16

全民K歌得高分教程大全
全民K歌得高分教程大全

本专题整合了全民K歌得高分技巧汇总,阅读专题下面的文章了解更多详细内容。

3

2026.01.16

C++ 单元测试与代码质量保障
C++ 单元测试与代码质量保障

本专题系统讲解 C++ 在单元测试与代码质量保障方面的实战方法,包括测试驱动开发理念、Google Test/Google Mock 的使用、测试用例设计、边界条件验证、持续集成中的自动化测试流程,以及常见代码质量问题的发现与修复。通过工程化示例,帮助开发者建立 可测试、可维护、高质量的 C++ 项目体系。

10

2026.01.16

java数据库连接教程大全
java数据库连接教程大全

本专题整合了java数据库连接相关教程,阅读专题下面的文章了解更多详细内容。

33

2026.01.15

Java音频处理教程汇总
Java音频处理教程汇总

本专题整合了java音频处理教程大全,阅读专题下面的文章了解更多详细内容。

15

2026.01.15

windows查看wifi密码教程大全
windows查看wifi密码教程大全

本专题整合了windows查看wifi密码教程大全,阅读专题下面的文章了解更多详细内容。

42

2026.01.15

浏览器缓存清理方法汇总
浏览器缓存清理方法汇总

本专题整合了浏览器缓存清理教程汇总,阅读专题下面的文章了解更多详细内容。

7

2026.01.15

ps图片相关教程汇总
ps图片相关教程汇总

本专题整合了ps图片设置相关教程合集,阅读专题下面的文章了解更多详细内容。

9

2026.01.15

ppt一键生成相关合集
ppt一键生成相关合集

本专题整合了ppt一键生成相关教程汇总,阅读专题下面的的文章了解更多详细内容。

6

2026.01.15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
CSS3 教程
CSS3 教程

共18课时 | 4.6万人学习

Pandas 教程
Pandas 教程

共15课时 | 0.9万人学习

麻省理工大佬Python课程
麻省理工大佬Python课程

共34课时 | 5.1万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号