Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

A better tr

Name: Anonymous 2010-08-22 14:39

private static readonly HashSet<char> badChars =
    new HashSet<char> { '!', '@', '#', '$', '%', '_' };

public static string CleanString(this string str)
{
    var result = new StringBuilder(str.Length);
    for (int i = 0; i < str.Length; i++)
    {
        if (!badChars.Contains(str[i]))
            result.Append(str[i]);
    }
    return result.ToString();
}


This algorithm makes use of the .NET 3.5 'HashSet' class to give O(1) look up time for detecting a bad char. This makes the overall algorithm O(n) rather than the O(nm) of your posted one (m being the number of bad chars); it also is lot a better with memory usage, as explained above.

Name: Anonymous 2010-08-22 17:38

>>5
That's but a small part of my aversion to tedious explanation. AFAIK to the average /prog/lodyte, >>2 might as well be using a Y-combinator--which I suspect is better understood around here... and at least it has an evocative name and possibly bewildering implementation to wield against people who would write it off.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List