soutsjjw
4/25/2019 - 2:38 AM

StringExpansion

字串擴充類別

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace StringExpansion
{
    public static class StringExpansion
    {
        public static IEnumerable<string> SplitByLength(this string str, int maxLength)
        {
            for (int index = 0; index < str.Length; index += maxLength)
            {
                yield return str.Substring(index, Math.Min(maxLength, str.Length - index));
            }
        }

        public static int RealyLength(this string str)
        {
            return System.Text.Encoding.Default.GetBytes(str).Length;
        }

        public static string Remove(this string str, string removeString)
        {
            return str.Replace(removeString, "");
        }

        public static int? ToNullableInt(this string s)
        {
            int i;
            if (int.TryParse(s, out i)) return i;
            return null;
        }

        public static string ToSafeSQL(this string SQL)
        {
            if (!string.IsNullOrEmpty(SQL))
                SQL = Regex.Replace(SQL, @"\b(?i)(exec(ute)?|select|update|insert|delete|drop|create|<|script|iframe|frame|frameset|>)\b|[;']|(-{2})|(/\*.*\*/)", string.Empty, RegexOptions.IgnoreCase);
            return SQL;
        }
        
        public static int RealyLength(this string str)
        {
            return System.Text.Encoding.Default.GetBytes(str).Length;
        }
        
        public static string HtmlDecode(this string str)
        {
            return System.Web.HttpUtility.HtmlDecode(str);
        }
    }
}