Can anyone think of an efficient way (time wise) to trim a few selected characters from the middle of a string?
Best I came up with was:
public static string Trim(this string word, IEnumerable<char> selectedChars)
{
string result = word;
foreach (char c in selectedChars)
result = result.Replace(c.ToString(), "");
return result;
}
But it is still too slow.
Best Solution
Two options spring to mind:
Here's the
StringBuilder
version:The regular expression option could be very efficient if you have a fixed set of chars you want to trim, and can build the regex once.
Something like: