There is a question on StackOverflow.com about better ways to remove characters in a string that are outside the range of 32 to 175.
Quite a few fancy solutions are provided:
The fastest solution is filtering while walkng through the string and build result using StringBuilder. But there are still places this can be improved. So here would be my solution:
static unsafe string TrimRange(string str, char from, char to) { int count = 0; for (int i = 0; i < str.Length; i++) { char ch = str[i]; if ((ch >= from) && (ch <= to)) { count++; } } if (count == 0) return String.Empty; if (count == str.Length) return str; char * result = stackalloc char[count]; count = 0; for (int i = 0; i < str.Length; i++) { char ch = str[i]; if ((ch >= from) && (ch <= to)) { result[count ++] = ch; } } return new String(result, 0, count); }
The idea is quite simple: allocate managed memory only when really need.