一些有用的UtilityExtend小方法

public static bool StartBy(this string thisValue, params string[] startBy)
        {
            foreach (string item in startBy)
            {
                if (thisValue.StartsWith(item))
                    return true;
            }

            return false;
        }


        public static bool StartBy(this string thisValue, string startBy, char splitSign)
        {
            string[] starts = startBy.Split(splitSign);
            if (starts.Length > 0)
                foreach (string item in starts)
                {
                    if (thisValue.StartsWith(item))
                        return true;
                }

            return false;
        }

        public static bool IsMatch(this string thisValue, string regexPattern)
        {
            return Regex.IsMatch(thisValue, regexPattern);
        }

       public static string ReplaceString(this string thisValue, string regexPattern, string replacement)
        {
            //Bug Fix
            if (!string.IsNullOrEmpty(thisValue))
            {
                return Regex.Replace(thisValue, regexPattern, replacement, RegexOptions.IgnoreCase);
            }
            return string.Empty;
        }


public static bool IsIn(this string thisValue, params string[] stringCollect)
        {
            if (stringCollect == null || stringCollect.Length <= 0)
                return false;

            return Array.IndexOf(stringCollect, thisValue) > -1;
        }


 public static T IfNullThen<T>(this object thisValue, T val)
        {
            return thisValue == null ? val : (T) thisValue;
        }

 public static string[] GetSubString(this string thisValue, string regexPattern)
        {
            string[] result = null;
            MatchCollection collection = Regex.Matches(thisValue, regexPattern, RegexOptions.IgnoreCase);
            if (collection.Count > 0)
            {
                result = new string[collection.Count];
                for (int i = 0; i < collection.Count; i++)
                {
                    result[i] = collection[i].Value;
                }
            }

            return result;
        }


public static string GetHashCode(this string thisValue, int leftLength)
        {
            string hashCode = Newegg.BigData.Framework.Common.ShareFunctions.GetHashCode(thisValue);
            return hashCode.Length >= 4 ? hashCode.Substring(0, leftLength) : hashCode;
        }


        public static string GetRowKeyHashCode(this string thisValue)
        {
            return thisValue.GetHashCode(4);
        }

  

原文地址:https://www.cnblogs.com/Wolfmanlq/p/4183404.html