当前位置: 首页 > 编程笔记 >

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

融烨磊
2023-03-14
本文向大家介绍C#常用的字符串扩展方法汇总,包括了C#常用的字符串扩展方法汇总的使用技巧和注意事项,需要的朋友参考一下

本文实例汇总了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#程序设计有所帮助。

 类似资料:
  • 本文向大家介绍C++常用字符串分割方法实例汇总,包括了C++常用字符串分割方法实例汇总的使用技巧和注意事项,需要的朋友参考一下 本文实例汇总了C++常用字符串分割方法,分享给大家供大家参考。具体分析如下: 我们在编程的时候经常会碰到字符串分割的问题,这里总结下,也方便我们以后查询使用。 一、用strtok函数进行字符串分割 原型: char *strtok(char *str, const cha

  • ES6加强了对Unicode的支持,并且扩展了字符串对象。 字符的Unicode表示法 JavaScript允许采用\uxxxx形式表示一个字符,其中“xxxx”表示字符的码点。 "\u0061" // "a" 但是,这种表示法只限于\u0000——\uFFFF之间的字符。超出这个范围的字符,必须用两个双字节的形式表达。 "\uD842\uDFB7" // "

  • 本文向大家介绍JAVA字符串拼接常见方法汇总,包括了JAVA字符串拼接常见方法汇总的使用技巧和注意事项,需要的朋友参考一下 字符串的拼接,常使用到的大概有4种方式: 1.直接使用"+"号 2.使用String的concat方法 3.使用StringBuilder的append方法 4.使用StringBuffer的append方法 由于String是final类型的,因此String对象都是属于不

  • 本文向大家介绍C#中的扩展方法,包括了C#中的扩展方法的使用技巧和注意事项,需要的朋友参考一下 扩展方法是静态方法,就像它们是扩展类型的实例方法一样被调用。使用扩展方法,您可以将方法添加到现有类型中,而无需创建新的派生类型,重新编译或修改原始类型。 以下是我们创建的扩展方法。 让我们看一个使用扩展方法的例子。 示例 输出结果

  • 本文向大家介绍C#中的任何扩展方法,包括了C#中的任何扩展方法的使用技巧和注意事项,需要的朋友参考一下 该扩展方法是System.Linq的namepspace的一部分。使用此方法,可以检查是否有任何元素符合特定条件。 首先,设置一个包含元素的数组- 以下是一个示例。它检查数组中的任何元素是否大于或等于20- 这是完整的代码- 示例 输出结果

  • 本文向大家介绍C#中的扩展方法详解,包括了C#中的扩展方法详解的使用技巧和注意事项,需要的朋友参考一下 扩展方法使你能够向现有类型“添加”方法,而无需创建新的派生类型、重新编译或以其他方式修改原始类型。 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用。 以上是msdn官网对扩展方法的描述,现在我通过一个情景例子来对此进行阐释。假设一个控制台程序class Program{}