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

C#常用的字符串扩展方法汇总

程序员文章站 2023-12-16 16:41:46
本文实例汇总了c#常用的字符串扩展方法,分享给大家供大家参考。具体如下: estring.cs文件如下: 复制代码 代码如下:/// ...

本文实例汇总了c#常用的字符串扩展方法,分享给大家供大家参考。具体如下:

estring.cs文件如下:

复制代码 代码如下:
/// <summary>
/// 扩展字符串类
/// </summary>
public static class estring
{
        #region 数据转换

        #region 转int
        /// <summary>
        /// 转int,失败返回0
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static int toint(this string t)
        {
            int n;
            if (!int.tryparse(t, out n))
                return 0;
            return n;
        }

        /// <summary>
        /// 转int,失败返回preturn
        /// </summary>
        /// <param name="e"></param>
        /// <param name="preturn">失败返回的值</param>
        /// <returns></returns>
        public static int toint(this string t, int preturn)
        {
            int n;
            if (!int.tryparse(t, out n))
                return preturn;
            return n;
        }

        /// <summary>
        /// 是否是int true:是 false:否
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static bool isint(this string t)
        {
            int n;
            return int.tryparse(t, out n);
        }
        #endregion

        #region 转int16
        /// <summary>
        /// 转int,失败返回0
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static int16 toint16(this string t)
        {
            int16 n;
            if (!int16.tryparse(t, out n))
                return 0;
            return n;
        }

        /// <summary>
        /// 转int,失败返回preturn
        /// </summary>
        /// <param name="e"></param>
        /// <param name="preturn">失败返回的值</param>
        /// <returns></returns>
        public static int16 toint16(this string t, int16 preturn)
        {
            int16 n;
            if (!int16.tryparse(t, out n))
                return preturn;
            return n;
        }

        /// <summary>
        /// 是否是int true:是 false:否
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static bool isint16(this string t)
        {
            int16 n;
            return int16.tryparse(t, out n);
        }
        #endregion

        #region 转byte
        /// <summary>
        /// 转byte,失败返回0
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static byte tobyte(this string t)
        {
            byte n;
            if (!byte.tryparse(t, out n))
                return 0;
            return n;
        }

        /// <summary>
        /// 转byte,失败返回preturn
        /// </summary>
        /// <param name="e"></param>
        /// <param name="preturn">失败返回的值</param>
        /// <returns></returns>
        public static byte tobyte(this string t, byte preturn)
        {
            byte n;
            if (!byte.tryparse(t, out n))
                return preturn;
            return n;
        }

        /// <summary>
        /// 是否是byte true:是 false:否
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static bool isbyte(this string t)
        {
            byte n;
            return byte.tryparse(t, out n);
        }
        #endregion

        #region 转long
        /// <summary>
        /// 转long,失败返回0
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static long tolong(this string t)
        {

            long n;
            if (!long.tryparse(t, out n))
                return 0;
            return n;
        }

        /// <summary>
        /// 转long,失败返回preturn
        /// </summary>
        /// <param name="e"></param>
        /// <param name="preturn">失败返回的值</param>
        /// <returns></returns>
        public static long tolong(this string t, long preturn)
        {
            long n;
            if (!long.tryparse(t, out n))
                return preturn;
            return n;
        }

        /// <summary>
        /// 是否是long true:是 false:否
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static bool islong(this string t)
        {
            long n;
            return long.tryparse(t, out n);
        }
        #endregion

        #region 转double
        /// <summary>
        /// 转int,失败返回0
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static double todouble(this string t)
        {
            double n;
            if (!double.tryparse(t, out n))
                return 0;
            return n;
        }

        /// <summary>
        /// 转double,失败返回preturn
        /// </summary>
        /// <param name="e"></param>
        /// <param name="preturn">失败返回的值</param>
        /// <returns></returns>
        public static double todouble(this string t, double preturn)
        {
            double n;
            if (!double.tryparse(t, out n))
                return preturn;
            return n;
        }

        /// <summary>
        /// 是否是double true:是 false:否
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static bool isdouble(this string t)
        {
            double n;
            return double.tryparse(t, out n);
        }
        #endregion

        #region 转decimal
        /// <summary>
        /// 转decimal,失败返回0
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static decimal todecimal(this string t)
        {
            decimal n;
            if (!decimal.tryparse(t, out n))
                return 0;
            return n;
        }

        /// <summary>
        /// 转decimal,失败返回preturn
        /// </summary>
        /// <param name="e"></param>
        /// <param name="preturn">失败返回的值</param>
        /// <returns></returns>
        public static decimal todecimal(this string t, decimal preturn)
        {
            decimal n;
            if (!decimal.tryparse(t, out n))
                return preturn;
            return n;
        }

        /// <summary>
        /// 是否是decimal true:是 false:否
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static bool isdecimal(this string t)
        {
            decimal n;
            return decimal.tryparse(t, out n);
        }
        #endregion

        #region 转datetime
        /// <summary>
        /// 转datetime,失败返回当前时间
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static datetime todatetime(this string t)
        {
            datetime n;
            if (!datetime.tryparse(t, out n))
                return datetime.now;
            return n;
        }

        /// <summary>
        /// 转datetime,失败返回preturn
        /// </summary>
        /// <param name="e"></param>
        /// <param name="preturn">失败返回的值</param>
        /// <returns></returns>
        public static datetime todatetime(this string t, datetime preturn)
        {
            datetime n;
            if (!datetime.tryparse(t, out n))
                return preturn;
            return n;
        }

        /// <summary>
        /// 转datetime,失败返回preturn
        /// </summary>
        /// <param name="e"></param>
        /// <param name="preturn">失败返回的值</param>
        /// <returns></returns>
        public static string todatetime(this string t, string pformat, string preturn)
        {
            datetime n;
            if (!datetime.tryparse(t, out n))
                return preturn;
            return n.tostring(pformat);
        }

        /// <summary>
        /// 转datetime,失败返回空
        /// </summary>
        /// <param name="e"></param>
        /// <param name="preturn">失败返回的值</param>
        /// <returns></returns>
        public static string todatetime(this string t, string pformat)
        {
            return t.todatetime(pformat, string.empty);
        }

        public static string toshortdatetime(this string t)
        {
            return t.todatetime("yyyy-mm-dd", string.empty);
        }

        /// <summary>
        /// 是否是datetime true:是 false:否
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static bool isdatetime(this string t)
        {
            datetime n;
            return datetime.tryparse(t, out n);
        }
        #endregion

        #region 与int[]相关
        /// <summary>
        /// 转int[],字符串以逗号(,)隔开,请确保字符串内容都合法,否则会出错
        /// </summary>
        /// <param name="pstr"></param>
        /// <returns></returns>
        public static int[] tointarr(this string t)
        {
            return t.tointarr(new char[] { ',' });
        }

        /// <summary>
        /// 转int[],字符串以逗号(,)隔开,请确保字符串内容都合法,否则会出错
        /// </summary>
        /// <param name="t"></param>
        /// <param name="psplit">隔开的</param>
        /// <returns></returns>
        public static int[] tointarr(this string t, char[] psplit)
        {
            if (t.length == 0)
            {
                return new int[] { };
            }

            string[] arrstr = t.split(psplit, stringsplitoptions.none);
            int[] istr = new int[arrstr.length];

            for (int i = 0; i < arrstr.length; i++)
                istr[i] = int.parse(arrstr[i]);

            return istr;
        }


        #endregion

        #region 过滤字符串的非int,重新组合成字符串
        /// <summary>
        /// 过滤字符串的非int,重新组合成字符串
        /// </summary>
        /// <param name="t"></param>
        /// <param name="psplit">分隔符</param>
        /// <returns></returns>
        public static string clearnoint(this string t, char psplit)
        {
            string sstr = string.empty;
            string[] arrstr = t.split(psplit);

            for (int i = 0; i < arrstr.length; i++)
            {
                string lsstr = arrstr[i];

                if (lsstr.isint())
                    sstr += lsstr + psplit;
                else
                    continue;
            }

            if (sstr.length > 0)
                sstr = sstr.trimend(psplit);

            return sstr;
        }

        /// <summary>
        /// 过滤字符串的非int,重新组合成字符串
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string clearnoint(this string t)
        {
            return t.clearnoint(',');
        }
        #endregion

        #region 是否可以转换成int[]
        /// <summary>
        /// 是否可以转换成int[],true:是,false:否
        /// </summary>
        /// <param name="t"></param>
        /// <param name="psplit">分隔符</param>
        /// <returns></returns>
        public static bool isintarr(this string t, char psplit)
        {
            string[] arrstr = t.split(psplit);
            bool b = true;

            for (int i = 0; i < arrstr.length; i++)
            {
                if (!arrstr[i].isint())
                {
                    b = false;
                    break;
                }
            }

            return b;
        }

        /// <summary>
        /// 是否可以转换成int[],true:是,false:否
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static bool isintarr(this string t)
        {
            return t.isintarr(',');
        }
        #endregion

        #endregion

        #region 载取左字符
        /// <summary>
        /// 载取左字符
        /// </summary>
        /// <param name="t"></param>
        /// <param name="plen">字符个数</param>
        /// <param name="preturn">超出时后边要加的返回的内容</param>
        /// <returns></returns>
        public static string left(this string t, int plen, string preturn)
        {
            if (t == null || t.length == 0)
                return string.empty;
            plen *= 2;
            int i = 0, j = 0;
            foreach (char c in t)
            {
                if (c > 127)
                {
                    i += 2;
                }
                else
                {
                    i++;
                }

                if (i > plen)
                {
                    return t.substring(0, j) + preturn;
                }

                j++;
            }

            return t;
        }

        public static string left(this string t, int plen)
        {
            return left(t, plen, string.empty);
        }

        public static string strleft(this string t, int plen)
        {
            if (t == null)
            {
                return "";
            }
            if (t.length > plen)
            {
                return t.substring(0, plen);
            }
            return t;
        }
        #endregion

        #region 删除文件名或路径的特殊字符

        private class clearpathunsafelist
        {
            public static readonly string[] unsafestr = { "/", "\\", ":", "*", "?", "\"", "<", ">", "|" };
        }

        /// <summary>
        /// 删除文件名或路径的特殊字符
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string clearpathunsafe(this string t)
        {
            foreach (string s in clearpathunsafelist.unsafestr)
            {
                t = t.replace(s, "");
            }

            return t;
        }
        #endregion

        #region 字符串真实长度 如:一个汉字为两个字节
        /// <summary>
        /// 字符串真实长度 如:一个汉字为两个字节
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static int lengthreal(this string s)
        {
            return encoding.default.getbytes(s).length;
        }
        #endregion

        #region 去除小数位最后为0的
        /// <summary>
        /// 去除小数位最后为0的
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static decimal cleardecimal0(this string t)
        {
            decimal d;
            if (decimal.tryparse(t, out d))
            {
                return decimal.parse(double.parse(d.tostring("g")).tostring());
            }
            return 0;
        }
        #endregion

        #region 进制转换
        /// <summary>
        /// 16进制转二进制
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string change16to2(this string t)
        {
            string binone = string.empty;
            string binall = string.empty;
            char[] nums = t.tochararray();
            for (int i = 0; i < nums.length; i++)
            {
                string number = nums[i].tostring();
                int num = int32.parse(number, system.globalization.numberstyles.hexnumber);

                binone = convert.tostring(num, 2).padleft(4, '0');
                binall = binall + binone;
            }
            return binall;
        }

        /// <summary>
        /// 二进制转十进制
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static int64 change2to10(this string t)
        {
            char[] arrc = t.tochararray();
            int64 all = 0, indexc = 1;
            for (int i = arrc.length - 1; i >= 0; i--)
            {
                if (arrc[i] == '1')
                {
                    all += indexc;
                }
                indexc = indexc * 2;
            }

            return all;
        }

        /// <summary>
        /// 二进制转换byte[]数组
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static byte[] change2tobytes(this string t)
        {
            list<byte> list = new list<byte>();

            char[] arrc = t.tochararray();
            byte n = 0;
            char c;
            int j = 0;
            //倒序获取位
            for (int i = arrc.length - 1; i >= 0; i--)
            {
                c = arrc[i];

                if (c == '1')
                {
                    n += convert.tobyte(math.pow(2, j));
                }
                j++;

                if (j % 8 == 0)
                {
                    list.add(n);
                    j = 0;
                    n = 0;
                }
            }

            //剩余最高位
            if (n > 0)
                list.add(n);

            byte[] arrb = new byte[list.count];

            int j1 = 0;
            //倒序
            for (int i = list.count - 1; i >= 0; i--)
            {
                arrb[j1] = list[i];
                j1++;
            }
            return arrb;
        }

        /// <summary>
        /// 二进制转化为索引id数据,从右到左
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static int[] change2toindex(this string t)
        {
            list<int> list = new list<int>();
            char[] arrc = t.tochararray();
            char c;
            int j = 0;

            //倒序获取位
            for (int i = arrc.length - 1; i >= 0; i--)
            {
                j++;
                c = arrc[i];

                if (c == '1')
                {
                    list.add(j);
                }
            }

            return list.toarray();
        }
        #endregion

        #region html url编码 解码
        /// <summary>
        /// html encode
        /// </summary>
        /// <param name="pstr"></param>
        /// <returns></returns>
        public static string htmlencode(this string t)
        {
            return httpcontext.current.server.htmlencode(t);
        }

        /// <summary>
        /// html decode
        /// </summary>
        /// <param name="pstr"></param>
        /// <returns></returns>
        public static string htmldecode(this string t)
        {
            return httpcontext.current.server.htmldecode(t);
        }

        /// <summary>
        /// url encode
        /// </summary>
        /// <param name="pstr"></param>
        /// <returns></returns>
        public static string urlencode(this string t)
        {
            return httpcontext.current.server.urlencode(t);
        }

        /// <summary>
        /// url decode
        /// </summary>
        /// <param name="pstr"></param>
        /// <returns></returns>
        public static string urldecode(this string t)
        {
            return httpcontext.current.server.urldecode(t);
        }
        #endregion

        #region 向客户端输出内容
        /// <summary>
        /// 向客户端输出内容
        /// </summary>
        /// <param name="t"></param>
        public static void write(this string t)
        {
            httpcontext.current.response.write(t);
        }

        /// <summary>
        /// 向客户端输出内容
        /// </summary>
        /// <param name="t"></param>
        public static void writeline(this string t)
        {
            httpcontext.current.response.write(t + "<br />");
        }
        #endregion
}

希望本文所述对大家的c#程序设计有所帮助。

上一篇:

下一篇: