ClrPlus.Core.Extensions.StringExtensions.Replace C# (CSharp) Method

Replace() public static method

Case insensitive version of String.Replace().
public static Replace ( this s, string oldValue, string newValue, System.StringComparison comparisonType ) : string
s this String that contains patterns to replace
oldValue string Pattern to find
newValue string New pattern to replaces old
comparisonType System.StringComparison String comparison type
return string
        public static string Replace(this string s, string oldValue, string newValue,
            StringComparison comparisonType) {
            if(s == null)
                return null;

            if(String.IsNullOrEmpty(oldValue))
                return s;

            var result = new StringBuilder(Math.Min(4096, s.Length));
            var pos = 0;

            while(true) {
                var i = s.IndexOf(oldValue, pos, comparisonType);
                if(i < 0)
                    break;

                result.Append(s, pos, i - pos);
                result.Append(newValue);

                pos = i + oldValue.Length;
            }
            result.Append(s, pos, s.Length - pos);

            return result.ToString();
        }
    }