how to replace characters in a array quickly

The reason that it spends so much time in that line is because the Contains method loops through the array to look for the character.

Put the characters in a HashSet<char> instead:

private static HashSet<char> badChars =
  new HashSet<char>(new char[] { '\x00', '\x09', '\x0A', '\x10' });

The code to check if the set contains the character looks the same as when looking in the array, but it uses the hash code of the character to look for it instead of looping through all the items in the array.

Alternatively, you could put the characters in a switch, that way the compiler would create an efficient comparison:

switch (buffer[i]]) {
  case '\x00':
  case '\x09':
  case '\x0A':
  case '\x10': buffer[i] = ' '; break;
}

If you have more characters (five or six IIRC), the compiler will actually create a hash table to look up the cases, so that would be similar to using a HashSet.

Leave a Comment