-2

Which is the most performance efficient way to remove multiple characters from a C# string? I have to remove all the spaces, hyphen from a C# string.

Thanks

Vishnu Y
  • 2,211
  • 4
  • 25
  • 38

3 Answers3

2

If you care about performance because the string can be quite large and/or the list of remove-characters is long you should use a StringBuilder.

StringBuilder sb = new StringBuilder(text.Length);
HashSet<Char> removeChars = new HashSet<Char>{ ' ', '-' };
foreach(Char c in text)
{
    if(!removeChars.Contains(c))
        sb.Append(c);
}
text = sb.ToString();

in most cases this should be sufficient:

foreach (String c in new[] { " ", "-" })
    text = text.Replace(c, "");
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

If the list of characters to remove is large (say more than 10 characters or so, but you'd have to do careful timing to check) then Tim's approach using a HashSet is likely to be the fastest.

However if the list of characters to remove is small, this code might be faster:

public static class StringExt
{
    public static string Remove(this string self, params char[] charsToRemove)
    {
        var result = new StringBuilder();

        foreach (char c in self)
            if (Array.IndexOf(charsToRemove, c) == -1)
                result.Append(c);

        return result.ToString();
    }
}

For your example (removing spaces and hyphen from a string) you could do the following (this removes tabs and newlines too, but just remove those parameters if you don't need them):

string original = "1 2\n3\t4-5";
string result = original.Remove(' ', '-', '\n', '\t');
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
-1
var input = @"-a b c-";

var index = 0;
var tmp = new char[input.Length];
foreach (var c in input)
{
    if (c != ' ' && c != '-')
    {
        tmp[index++] = c;
    }
}

var result = new string(tmp, 0, index);
The_Black_Smurf
  • 5,178
  • 14
  • 52
  • 78
Petar Petrov
  • 2,729
  • 2
  • 22
  • 17
  • You are reinventing a `StringBuilder` ;-) – Tim Schmelter Aug 08 '14 at 09:47
  • No, I am not. StringBuilder is a class with a char array field and some other fields - length, offset, capacity, ... Most performance efficient in this case is just char[]. Also Append(char c) is performing a check for a possible internal re-size. – Petar Petrov Aug 08 '14 at 09:57
  • that's just micro optimization which costs readability and maintainability. – Tim Schmelter Aug 08 '14 at 10:01
  • Sometimes performance hurts readability and maintainability but if this code is in a separate method I don't think it will. – Petar Petrov Aug 08 '14 at 10:10
  • You should really not care about the performance of `1 < 2`. That's not a reason to reinvent an existing class which supports many other things too. It might even be better to just use `String.Replace` to gain even more readability and fail-safety: `foreach (String c in new[] { " ", "-" }) text = text.Replace(c, "");`. I suspect that OP has not even noticed a performance issue but just asks for the most performant way beforehand. – Tim Schmelter Aug 08 '14 at 10:15
  • 1
    But this guy cares and he asks for "most performance efficient way" and I think I give it. The Replace version is a complete waste it will create one unnecessary temp string(the one without spaces). With that said I'm ending this futile debate. – Petar Petrov Aug 08 '14 at 10:32