SoFunction
Updated on 2025-03-07

Summary of commonly used string extension methods in C#

/// <summary>
/// Extend string class
/// </summary>
public static class EString
{
#region Data conversion

#region to Int
        /// <summary>
/// If you turn to Int, return 0 if you fail
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static int ToInt(this string t)
        {
            int n;
            if (!(t, out n))
                return 0;
            return n;
        }

        /// <summary>
/// If you turn to Int, return to pReturn if you fail
        /// </summary>
        /// <param name="e"></param>
/// <param name="pReturn">Returned value failed</param>
        /// <returns></returns>
        public static int ToInt(this string t, int pReturn)
        {
            int n;
            if (!(t, out n))
                return pReturn;
            return n;
        }

        /// <summary>
/// Whether it is Int true: Yes false: No
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static bool IsInt(this string t)
        {
            int n;
            return (t, out n);
        }
        #endregion

#region to Int16
        /// <summary>
/// If you turn to Int, return 0 if you fail
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static Int16 ToInt16(this string t)
        {
            Int16 n;
            if (!(t, out n))
                return 0;
            return n;
        }

        /// <summary>
/// If you turn to Int, return to pReturn if you fail
        /// </summary>
        /// <param name="e"></param>
/// <param name="pReturn">Returned value failed</param>
        /// <returns></returns>
        public static Int16 ToInt16(this string t, Int16 pReturn)
        {
            Int16 n;
            if (!(t, out n))
                return pReturn;
            return n;
        }

        /// <summary>
/// Whether it is Int true: Yes false: No
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static bool IsInt16(this string t)
        {
            Int16 n;
            return (t, out n);
        }
        #endregion

#region to byte
        /// <summary>
/// If you turn byte, return 0 if you fail
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static byte Tobyte(this string t)
        {
            byte n;
            if (!(t, out n))
                return 0;
            return n;
        }

        /// <summary>
/// If byte is turned, the failure is returned to pReturn
        /// </summary>
        /// <param name="e"></param>
/// <param name="pReturn">Returned value failed</param>
        /// <returns></returns>
        public static byte Tobyte(this string t, byte pReturn)
        {
            byte n;
            if (!(t, out n))
                return pReturn;
            return n;
        }

        /// <summary>
/// Whether it is byte true: Yes false: No
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static bool Isbyte(this string t)
        {
            byte n;
            return (t, out n);
        }
        #endregion

#region to Long
        /// <summary>
/// If you turn Long, return 0 if you fail
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static long ToLong(this string t)
        {

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

        /// <summary>
/// If you turn Long, return to pReturn if you fail
        /// </summary>
        /// <param name="e"></param>
/// <param name="pReturn">Returned value failed</param>
        /// <returns></returns>
        public static long ToLong(this string t, long pReturn)
        {
            long n;
            if (!(t, out n))
                return pReturn;
            return n;
        }

        /// <summary>
/// Whether it is Long true: Yes false: No
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static bool IsLong(this string t)
        {
            long n;
            return (t, out n);
        }
        #endregion

#region to Double
        /// <summary>
/// If you turn to Int, return 0 if you fail
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static double ToDouble(this string t)
        {
            double n;
            if (!(t, out n))
                return 0;
            return n;
        }

        /// <summary>
/// If you turn to Double, return to pReturn if you fail
        /// </summary>
        /// <param name="e"></param>
/// <param name="pReturn">Returned value failed</param>
        /// <returns></returns>
        public static double ToDouble(this string t, double pReturn)
        {
            double n;
            if (!(t, out n))
                return pReturn;
            return n;
        }

        /// <summary>
/// Whether it is Double true: Yes false: No
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static bool IsDouble(this string t)
        {
            double n;
            return (t, out n);
        }
        #endregion

#region to Decimal
        /// <summary>
/// If you turn to Decimal, return 0 if you fail
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static decimal ToDecimal(this string t)
        {
            decimal n;
            if (!(t, out n))
                return 0;
            return n;
        }

        /// <summary>
/// If you turn to Decimal, return to pReturn if you fail
        /// </summary>
        /// <param name="e"></param>
/// <param name="pReturn">Returned value failed</param>
        /// <returns></returns>
        public static decimal ToDecimal(this string t, decimal pReturn)
        {
            decimal n;
            if (!(t, out n))
                return pReturn;
            return n;
        }

        /// <summary>
/// Whether it is Decimal true: Yes false: No
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static bool IsDecimal(this string t)
        {
            decimal n;
            return (t, out n);
        }
        #endregion

#region to DateTime
        /// <summary>
/// Return to DateTime, fail to return to the current time
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(this string t)
        {
            DateTime n;
            if (!(t, out n))
                return ;
            return n;
        }

        /// <summary>
/// Return to DateTime, fail to return to pReturn
        /// </summary>
        /// <param name="e"></param>
/// <param name="pReturn">Returned value failed</param>
        /// <returns></returns>
        public static DateTime ToDateTime(this string t, DateTime pReturn)
        {
            DateTime n;
            if (!(t, out n))
                return pReturn;
            return n;
        }

        /// <summary>
/// Return to DateTime, fail to return to pReturn
        /// </summary>
        /// <param name="e"></param>
/// <param name="pReturn">Returned value failed</param>
        /// <returns></returns>
        public static string ToDateTime(this string t, string pFormat, string pReturn)
        {
            DateTime n;
            if (!(t, out n))
                return pReturn;
            return (pFormat);
        }

        /// <summary>
/// Return to DateTime, fail to return to empty
        /// </summary>
        /// <param name="e"></param>
/// <param name="pReturn">Returned value failed</param>
        /// <returns></returns>
        public static string ToDateTime(this string t, string pFormat)
        {
            return (pFormat, );
        }

        public static string ToShortDateTime(this string t)
        {
            return ("yyyy-MM-dd", );
        }

        /// <summary>
/// Whether it is DateTime true: Yes false: No
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static bool IsDateTime(this string t)
        {
            DateTime n;
            return (t, out n);
        }
        #endregion

#region related to int[]
        /// <summary>
/// Turn to int[], the string is separated by commas (,). Please make sure that the string content is legal, otherwise an error will occur.
        /// </summary>
        /// <param name="pStr"></param>
        /// <returns></returns>
        public static int[] ToIntArr(this string t)
        {
            return (new char[] { ',' });
        }

        /// <summary>
/// Turn to int[], the string is separated by commas (,). Please make sure that the string content is legal, otherwise an error will occur.
        /// </summary>
        /// <param name="t"></param>
/// <param name="pSplit">separated</param>
        /// <returns></returns>
        public static int[] ToIntArr(this string t, char[] pSplit)
        {
            if ( == 0)
            {
                return new int[] { };
            }

            string[] ArrStr = (pSplit, );
            int[] iStr = new int[];

            for (int i = 0; i < ; i++)
                iStr[i] = (ArrStr[i]);

            return iStr;
        }


        #endregion

#region Filter non-int of strings and recombinate them into strings
        /// <summary>
/// Filter non-int of strings and recombine them into strings
        /// </summary>
        /// <param name="t"></param>
/// <param name="pSplit">delimiter</param>
        /// <returns></returns>
        public static string ClearNoInt(this string t, char pSplit)
        {
            string sStr = ;
            string[] ArrStr = (pSplit);

            for (int i = 0; i < ; i++)
            {
                string lsStr = ArrStr[i];

                if (())
                    sStr += lsStr + pSplit;
                else
                    continue;
            }

            if ( > 0)
                sStr = (pSplit);

            return sStr;
        }

        /// <summary>
/// Filter non-int of strings and recombine them into strings
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string ClearNoInt(this string t)
        {
            return (',');
        }
        #endregion

Can #region be converted to int[]
        /// <summary>
/// Is it possible to convert to int[], true: Yes, false: No
        /// </summary>
        /// <param name="t"></param>
/// <param name="pSplit">delimiter</param>
        /// <returns></returns>
        public static bool IsIntArr(this string t, char pSplit)
        {
            string[] ArrStr = (pSplit);
            bool b = true;

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

            return b;
        }

        /// <summary>
/// Is it possible to convert to int[], true: Yes, false: No
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static bool IsIntArr(this string t)
        {
            return (',');
        }
        #endregion

        #endregion

#region Load the left character
        /// <summary>
/// Load the left character
        /// </summary>
        /// <param name="t"></param>
/// <param name="pLen">number of characters</param>
/// <param name="pReturn">The returned content to be added after the expiration</param>
        /// <returns></returns>
        public static string Left(this string t, int pLen, string pReturn)
        {
            if (t == null || == 0)
                return ;
            pLen *= 2;
            int i = 0, j = 0;
            foreach (char c in t)
            {
                if (c > 127)
                {
                    i += 2;
                }
                else
                {
                    i++;
                }

                if (i > pLen)
                {
                    return (0, j) + pReturn;
                }

                j++;
            }

            return t;
        }

        public static string Left(this string t, int pLen)
        {
            return Left(t, pLen, );
        }

        public static string StrLeft(this string t, int pLen)
        {
            if (t == null)
            {
                return "";
            }
            if ( > pLen)
            {
                return (0, pLen);
            }
            return t;
        }
        #endregion

#region Delete special characters of file name or path

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

        /// <summary>
/// Delete special characters of file name or path
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string ClearPathUnsafe(this string t)
        {
            foreach (string s in )
            {
                t = (s, "");
            }

            return t;
        }
        #endregion

#region The true length of the string, such as: one Chinese character is two bytes
        /// <summary>
/// The true length of the string, such as: one Chinese character is two bytes
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static int LengthReal(this string s)
        {
            return (s).Length;
        }
        #endregion

#region Removes the decimal places with the last 0
        /// <summary>
/// Remove the decimal places with the last 0
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static decimal ClearDecimal0(this string t)
        {
            decimal d;
            if ((t, out d))
            {
                return ((("g")).ToString());
            }
            return 0;
        }
        #endregion

#region
        /// <summary>
/// Hexadecimal to binary
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string Change16To2(this string t)
        {
            String BinOne = ;
            String BinAll = ;
            char[] nums = ();
            for (int i = 0; i < ; i++)
            {
                string number = nums[i].ToString();
                int num = (number, );

                BinOne = (num, 2).PadLeft(4, '0');
                BinAll = BinAll + BinOne;
            }
            return BinAll;
        }

        /// <summary>
/// Binary to decimal
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static Int64 Change2To10(this string t)
        {
            char[] arrc = ();
            Int64 all = 0, indexC = 1;
            for (int i = - 1; i >= 0; i--)
            {
                if (arrc[i] == '1')
                {
                    all += indexC;
                }
                indexC = indexC * 2;
            }

            return all;
        }

        /// <summary>
/// Binary conversion byte[] array
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static byte[] Change2ToBytes(this string t)
        {
            List<byte> list = new List<byte>();

            char[] arrc = ();
            byte n = 0;
            char c;
            int j = 0;
//Reverse order to obtain bits
            for (int i = - 1; i >= 0; i--)
            {
                c = arrc[i];

                if (c == '1')
                {
                    n += ((2, j));
                }
                j++;

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

//The remaining highest position
            if (n > 0)
                (n);

            byte[] arrb = new byte[];

            int j1 = 0;
//Inverse order
            for (int i = - 1; i >= 0; i--)
            {
                arrb[j1] = list[i];
                j1++;
            }
            return arrb;
        }

        /// <summary>
/// Binary is converted into index id data, from right to left
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static int[] Change2ToIndex(this string t)
        {
            List<int> list = new List<int>();
            char[] arrc = ();
            char c;
            int j = 0;

//Reverse order to obtain bits
            for (int i = - 1; i >= 0; i--)
            {
                j++;
                c = arrc[i];

                if (c == '1')
                {
                    (j);
                }
            }

            return ();
        }
        #endregion

#region html url encoding decoding
        /// <summary>
        /// Html Encode
        /// </summary>
        /// <param name="pStr"></param>
        /// <returns></returns>
        public static string HtmlEncode(this string t)
        {
            return (t);
        }

        /// <summary>
        /// Html Decode
        /// </summary>
        /// <param name="pStr"></param>
        /// <returns></returns>
        public static string HtmlDecode(this string t)
        {
            return (t);
        }

        /// <summary>
        /// URL Encode
        /// </summary>
        /// <param name="pStr"></param>
        /// <returns></returns>
        public static string URLEncode(this string t)
        {
            return (t);
        }

        /// <summary>
        /// URL Decode
        /// </summary>
        /// <param name="pStr"></param>
        /// <returns></returns>
        public static string URLDecode(this string t)
        {
            return (t);
        }
        #endregion

#region Output content to the client
        /// <summary>
//// Output content to the client
        /// </summary>
        /// <param name="t"></param>
        public static void Write(this string t)
        {
            (t);
        }

        /// <summary>
//// Output content to the client
        /// </summary>
        /// <param name="t"></param>
        public static void WriteLine(this string t)
        {
            (t + "<br />");
        }
        #endregion
}