This seems to be really simple, so why am I writing this blog post?
Everyone I asked, had a different idea to achieve this, the first idea was to use Regular Expressions, which is really straight forward and simple, but there are several other methods with quite different performance.
Let’s have a look at all methods that came in mind:
1. String Replace
2. Regex
3. split directly with whitespace character
4. split & join each character
5. use a StringBuilder
(this list may not be complete, but I focused on these methods)
// string replace Stopwatch sw = Stopwatch.StartNew(); int n = 10000; for (int i = 0; i < n; i++) { var strRes = testString.Replace(" ", ""); strRes = strRes.Replace("\t", ""); strRes = strRes.Replace("\n", ""); strRes = strRes.Replace("\r", ""); //if (expected != strRes) // throw new Exception(); } sw.Stop(); Debug.WriteLine(sw.Elapsed); // regex replace sw.Restart(); for (int i = 0; i < n; i++) { var strRes = Regex.Replace(testString, "\\s+", ""); //if (expected != strRes) // throw new Exception(); } sw.Stop(); Debug.WriteLine(sw.Elapsed); // split sw.Restart(); for (int i = 0; i < n; i++) { var parts = testString.Split(new[] { '\t', '\r', '\n', ' ' }, StringSplitOptions.RemoveEmptyEntries); //var parts = testString.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries); var strRes = string.Join("", parts); //if (expected != strRes) // throw new Exception(); } sw.Stop(); Debug.WriteLine(sw.Elapsed); // split 2 sw.Restart(); for (int i = 0; i < n; i++) { var parts = testString.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries); var strRes = string.Join("", parts); //if (expected != strRes) // throw new Exception(); } sw.Stop(); Debug.WriteLine(sw.Elapsed); // StringBuilder sw.Restart(); for (int i = 0; i < n; i++) { StringBuilder resSb = new StringBuilder(); foreach (var chr in testString) { if (chr != '\t' && chr != '\r' && chr != '\n' && chr != ' ') { resSb.Append(chr); } } var strRes = resSb.ToString(); //if (expected != strRes) // throw new Exception(); } sw.Stop(); Debug.WriteLine(sw.Elapsed);
The Results
1. String Replace: 41ms
2. Regex: 113ms
3. split directly with whitespace character: 27ms
4. split & join each character: 27ms
5. use a StringBuilder: 9ms