SYSK 322: RemoveChars Function For Your Utilities Assembly


Can you think of a case where you’d like to remove more than one character from a string?  Some examples include ‘\r’ and ‘\n’ as a carriage return + line feed, or ‘$’, ‘,’ and ‘ ‘ for currency data entry fields…


 


Of course you could call string.Replace as many times as there are number of characters you want to strip out…  or, you could use the function below to do the job.


 


public static string RemoveChars(string text, char[] removeChars)


{            


    List<char> result = new List<char>();


   


    foreach(char c in text.ToCharArray())


    {


        bool remove = false;


        foreach (char ic in removeChars)


        {


            if (c == ic)


            {


                remove = true;


                break;


            }


        }


 


        if (remove == false)


            result.Add(c);


    }


 


    return new string(result.ToArray());


}


 


 


Comments (3)

  1. C. Watford says:

    I wondered why you used a List<char> instead of a string, so I went and investigated, making different versions of RemoveChars. Yours performed extremely well, beating out implementations with StringBuilder and string. However, there are 3 subtle improvements which can be made to your version which translate to a real world speed increase.

    1). params char[] removeChars instead of char[] removeChars. It doesn’t add a speed increase, but makes the function nicer to use.

    2). foreach(char c in text) instead of text.ToCharArray(). Provides a 5% speed boost.

    3). new List<char>(text.Length). Provides a 20% speed boost.

    100,000 trials with ‘r’,’n’ removal on 5 strings from your blog

    —————————————-

    RemoveChars (yours): 1.0

    RemoveChars (just with params): 1.0

    RemoveChars (with string): 0.13

    RemoveChars (with StringBuilder): 0.93

    RemoveChars (with no ToCharArray): 1.04

    RemoveChars (with no ToCharArray and text.Length): 1.25

    25% speed boost with those 2 modifications, and a surprising loss for StringBuilder and string. I would have never thought to use a List<char> because of it’s (well my) perceived overhead. I’m going to have to rethink a lot of my utility functions after seeing this.

  2. Jon Kruger says:

    Is this faster than just using String.Replace()?  

  3. C. Watford says:

    String.Replace() is O(n), you would have to call it removeChars.Length times. Plus you cannot call String.Replace(char, ”), you’d have to ToString each char being passed. Plus there are the intermediate strings being created. Odds are the speed should be abysmal.

    However, in practice for small numbers of replacement characters, foreach using String.Replace() is really fast, much faster than any of the methods Irene or I have shown. However, once the number of characters you need to replace increases, slowly our methods overtake a naive String.Replace().

    Remember, it is important to get the functionality correct first, and then optimize the method for your application later.

Skip to main content