Advertisement
ivandrofly

Case Sensitive Replace

Jul 27th, 2015
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.42 KB | None | 0 0
  1.        static public string Replace(string original, string pattern, string replacement, StringComparison comparisonType)
  2.         {
  3.             return Replace(original, pattern, replacement, comparisonType, -1);
  4.         }
  5.        
  6.         static public string Replace(string original, string pattern, string replacement, StringComparison comparisonType, int stringBuilderInitialSize)
  7.         {
  8.             if (original == null)
  9.             {
  10.                 return null;
  11.             }
  12.  
  13.             if (String.IsNullOrEmpty(pattern))
  14.             {
  15.                 return original;
  16.             }
  17.  
  18.  
  19.             int posCurrent = 0;
  20.             int lenPattern = pattern.Length;
  21.             int idxNext = original.IndexOf(pattern, comparisonType);
  22.             StringBuilder result = new StringBuilder(stringBuilderInitialSize < 0 ? Math.Min(4096, original.Length) : stringBuilderInitialSize);
  23.  
  24.             while (idxNext >= 0)
  25.             {
  26.                 result.Append(original, posCurrent, idxNext - posCurrent);
  27.                 result.Append(replacement);
  28.  
  29.                 posCurrent = idxNext + lenPattern;
  30.  
  31.                 idxNext = original.IndexOf(pattern, posCurrent, comparisonType);
  32.             }
  33.  
  34.             result.Append(original, posCurrent, original.Length - posCurrent);
  35.  
  36.             return result.ToString();
  37.         }
  38.  
  39. // ============================================================ Example 2
  40. private static string ReplaceEx(string original, string pattern, string replacement)
  41. {
  42.     int count, position0, position1;
  43.     count = position0 = position1 = 0;
  44.     string upperString = original.ToUpper();
  45.     string upperPattern = pattern.ToUpper();
  46.     int inc = (original.Length/pattern.Length) * (replacement.Length-pattern.Length);
  47.    
  48.    
  49.     char [] chars = new char[original.Length + Math.Max(0, inc)];
  50.     while((position1 = upperString.IndexOf(upperPattern, position0)) != -1)
  51.     {
  52.         for ( int i=position0 ; i < position1 ; ++i )
  53.             chars[count++] = original[i];
  54.         for ( int i=0 ; i < replacement.Length ; ++i )
  55.             chars[count++] = replacement[i];
  56.         position0 = position1+pattern.Length;
  57.     }
  58.     if ( position0 == 0 ) return original;
  59.     for ( int i=position0 ; i < original.Length ; ++i )
  60.         chars[count++] = original[i];
  61.     return new string(chars, 0, count);
  62. }
  63. //=============================================================
  64. // Souce: Internet
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement