Meep

mau's minmax removal improved

Jul 29th, 2021 (edited)
1,071
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // http://rextester.com/WQYT17544
  2.  
  3. // Time elapsed (Regex Replace):  00:00:00.9082347 String: 4lph num
  4. // Time elapsed (Switch Replace): 00:00:00.1781592 String: 4lph num
  5. // Time elapsed (Switch Builder): 00:00:00.0172757 String: 4lph num
  6.  
  7. public static string RemoveNonAlphanumericQuicker(string input)
  8. {
  9.     var builder = new StringBuilder();
  10.    
  11.     for (var i = 0; i < input.Length; i++)
  12.     {
  13.         switch (input[i])
  14.         {
  15.             case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
  16.             case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
  17.             case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
  18.             case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B':
  19.             case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I':
  20.             case 'J': case 'K': case 'L': case 'M': case 'N': case 'O': case 'P':
  21.             case 'Q': case 'R': case 'S': case 'T': case 'U': case 'V': case 'W':
  22.             case 'X': case 'Y': case 'Z': case '0': case '1': case '2': case '3':
  23.             case '4': case '5': case '6': case '7': case '8': case '9': case ' ':
  24.                 builder.Append(input[i]);
  25.                 continue;
  26.  
  27.             default:
  28.                 continue;
  29.         }
  30.     }
  31.    
  32.     return builder.ToString();
  33. }
RAW Paste Data