栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

C# 模拟java lang3包实现String类扩展

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C# 模拟java lang3包实现String类扩展

java程序员都知道apache.commons.lang3包下面有很多String类的扩展,非常好用。然而C#这边,apache就不那么给力了。
怎么办?只能自己动手丰衣足食。
上代码:

 public static class StringUtil
 {
        public const int INDEX_NOT_FOUND = -1;
		
        public static int IndexOfIgnoreCase(this string str, string searchStr, int start = 0)
        {
            if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(searchStr))
            {
                return INDEX_NOT_FOUND;
            }
            if (start < 0)
            {
                return INDEX_NOT_FOUND;
            }
            int endLimit = str.Length - searchStr.Length + 1;
            if (start > endLimit || str.Length <= start)
            {
                return INDEX_NOT_FOUND;
            }
            if (searchStr.Length == 0)
            {
                return start;
            }
            var res = str.Substring(start).ToLower();
            return res.IndexOf(searchStr.ToLower());
        }

        public static int LastIndexOfIgnoreCase(this string str, string searchStr, int start = 0)
        {
            if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(searchStr))
            {
                return INDEX_NOT_FOUND;
            }
            if (start > str.Length - searchStr.Length)
            {
                start = str.Length - searchStr.Length;
            }
            if (start < 0 || str.Length <= start)
            {
                return INDEX_NOT_FOUND;
            }
            if (searchStr.Length == 0)
            {
                return start;
            }
            var res = str.Substring(start).ToLower();
            return res.LastIndexOf(searchStr.ToLower());
        }

        public static bool ContainsIgnoreCase(this string str, string searchStr)
        {
            if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(searchStr))
            {
                return false;
            }
            return str.ToLower().Contains(searchStr.ToLower());
        }

        public static bool StartsWithIgnoreCase(this string str, string searchStr)
        {
            if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(searchStr))
            {
                return false;
            }
            return str.ToLower().StartsWith(searchStr.ToLower());
        }


        public static bool EndWithIgnoreCase(this string str, string searchStr)
        {
            if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(searchStr))
            {
                return false;
            }
            return str.ToLower().EndsWith(searchStr.ToLower());
        }

        public static bool EqualsIgnoreCase(this string str, string searchStr)
        {
            if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(searchStr))
            {
                return false;
            }
            if (str.Length != searchStr.Length)
            {
                return false;
            }
            return str.ToLower().Equals(searchStr.ToLower());
        }

        public static string ReplaceFirst(this string value, string oldValue, string newValue)
        {
            if (string.IsNullOrEmpty(oldValue))
                return value;

            int idx = value.IndexOfIgnoreCase(oldValue);
            if (idx == -1)
                return value;
            value = value.Remove(idx, oldValue.Length);
            return value.Insert(idx, newValue);
        }

        public static string RelaceLast(this string value , string oldValue, string newValue)
        {
            if (string.IsNullOrEmpty(oldValue))
                return value;

            int idx = value.LastIndexOfIgnoreCase(oldValue);
            if (idx == -1)
                return value;
            value = value.Remove(idx, oldValue.Length);
            return value.Insert(idx, newValue);
        }

        public static string ReplaceIgnoreCase(this string value, string oldValue, string newValue)
        {
            if (string.IsNullOrEmpty(oldValue))
                return value;

            oldValue = oldValue.ToLower();
            int start = 0;
            int end = value.IndexOfIgnoreCase(oldValue, start);
            if (end == INDEX_NOT_FOUND)
            {
                return value;
            }
            return Regex.Replace(value, oldValue, newValue, RegexOptions.IgnoreCase);
        }

        public static string[] SplitIgnoreCase(this string value,string pattern)
        {
            if (value.IndexOfIgnoreCase(pattern) == -1)
            {
                return new string[] { value };
            }
            return Regex.Split(value, pattern, RegexOptions.IgnoreCase);
        }

        public static int ContainsNumIgnoreCase(this string value, string pattern,bool IgnoreCase=true)
        {
            if (IgnoreCase)
            {
                return Regex.Matches(value, pattern, RegexOptions.IgnoreCase).Count;
            }
            else
            {
                return Regex.Matches(value, pattern, RegexOptions.None).Count;
            }
        }

        public static int IndexOfCount(this string value,string pattern, int count=0)
        {
            var rep =string.Empty;
            int idx =value.IndexOfIgnoreCase(pattern);
            if (idx == -1) return -1;
            for(int i = 0; i < pattern.Length; i++)
            {
                rep += "#";
            }
            for(int i = 0; i < count; i++)
            {
                value = value.ReplaceFirst(pattern, rep);  
            }
            return value.IndexOfIgnoreCase(pattern);
        }

        public static string SubStr(this string value,int start,int len)
        {
            if(len == -1)
            {
                len = value.Length;
            }
            return value.Substring(start, len);
        }
    }

个人感觉C#的扩展比java略胜一筹,扩展方法调用更加方便,比如:

//调用上面的扩展方法
var str = sql.Substring(0, sql.IndexOfIgnoreCase("="));

自己项目中实测可用,若有不足之处,欢迎指正!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1032625.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号