Share via


Problem: Better way to remove characters that aren't 32 to 175

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:

  1. Regex.Replace(myString, @"[^\x20-\xaf]+", "");
  2. new string(myString.Where(c => (c >= 32) && (c <= 175)).ToArray());
  3. string text = (from c in myString let i = (int) c where i < 32 && i > 175 select c) .Aggregate("", (current, c) => current + c);   

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.