How about this...
public string RemoveMultiSpace(string test)
{
var words = test.Split(new char[] { ' ' },
StringSplitOptions.RemoveEmptyEntries);
return string.Join(" ", words);
}
Test case run with NUnit:
Test time is in milliseconds.
Regex Test time: 338,8885
RemoveMultiSpace Test time: 78,9335
private static readonly Regex regex_select_all_multiple_whitespace_chars =
new Regex(@"\s+", RegexOptions.Compiled);
[Test]
public void Test()
{
string startString = "A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F ";
string cleanString;
Trace.WriteLine("Regex Test start");
int count = 10000;
Stopwatch timer = new Stopwatch();
timer.Start();
for (int i = 0; i < count; i++)
{
cleanString = regex_select_all_multiple_whitespace_chars.Replace(startString, " ");
}
var elapsed = timer.Elapsed;
Trace.WriteLine("Regex Test end");
Trace.WriteLine("Regex Test time: " + elapsed.TotalMilliseconds);
Trace.WriteLine("RemoveMultiSpace Test start");
timer = new Stopwatch();
timer.Start();
for (int i = 0; i < count; i++)
{
cleanString = RemoveMultiSpace(startString);
}
elapsed = timer.Elapsed;
Trace.WriteLine("RemoveMultiSpace Test end");
Trace.WriteLine("RemoveMultiSpace Test time: " + elapsed.TotalMilliseconds);
}
public string RemoveMultiSpace(string test)
{
var words = test.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
return string.Join(" ", words);
}
Edit:
Made some more tests and added Guffa´s method "RemoveDuplicateSpaces" based on StringBuilder.
So my conclusion is that the StringBuilder method is faster when there is a lot of spaces, but with less spaces the string split method is slightly faster.
Cleaning file with about 30000 lines, 10 iterations
RegEx time elapsed: 608,0623
RemoveMultiSpace time elapsed: 239,2049
RemoveDuplicateSpaces time elapsed: 307,2044
Cleaning string, 10000 iterations:
A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F
RegEx time elapsed: 590,3626
RemoveMultiSpace time elapsed: 159,4547
RemoveDuplicateSpaces time elapsed: 137,6816
Cleaning string, 10000 iterations:
A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F A B C D E F
RegEx time elapsed: 290,5666
RemoveMultiSpace time elapsed: 64,6776
RemoveDuplicateSpaces time elapsed: 52,4732