zoukankan      html  css  js  c++  java
  • 字符串函数扩展 replace,indexof

    public static class StringExtensions
        {
            public static int IndexOfTimes(this string s, string value, int times)
            {
                return s.IndexOfTimes(value, 0, times);
            }
            public static int IndexOfTimes(this string s, string value, int startindex, int times)
            {
                if (string.IsNullOrEmpty(s)) return -1;
                startindex = startindex < 0 ? 0 : startindex;
                if (times <= 0)
                    return -1;
                int at = 0;
                int count = 0;
                while (startindex < s.Length)
                {
                    at = s.IndexOf(value, startindex);
                    if (at == -1)
                        return -1;
                    count++;
                    if (count == times)
                        return at;
                    startindex = at + 1;
                }
                return -1;
            }
    
            public static string ReplaceFirst(this string s, string oldValue, string newValue)
            {
                return s.ReplaceTime(oldValue, newValue, 1);
            }
    
            public static string ReplaceTime(this string s, string oldValue, string newValue, int times)
            {
                int pos=s.IndexOfTimes(oldValue, times);
                if (pos < 0)
                    return s;
                if (string.IsNullOrEmpty(newValue))
                    return "";
    
                s = s.Remove(pos, oldValue.Length);
                s = s.Insert(pos, newValue);
                return s;
            }
        }
    

  • 相关阅读:
    ehcache 使用
    android 换肤 apk
    ant 打不同渠道包
    strawberry perl
    rest 网络设计开发,降低复杂性设计方案
    android 进度条
    android 算定义布局xml
    ant 自动打包
    c# 调用cmd 输出 阻塞 解决
    web service2
  • 原文地址:https://www.cnblogs.com/asingna/p/1734408.html
Copyright © 2011-2022 走看看