Hello Pavel,
1) You can use Regex.Replace:
Regex.Replace(s, "\x20+", "\x20");
That suggests only a space... there are a lot of blanks a user might be able
to enter (especially when copy pasting)...
I'd suggest
private static Regex rx = new Regex("\s+", RegexOptions.Compiled);
and then use this in your method:
rx.Replace(userInput, [ ]);
this will search and replace every occurance of multiple whitespaces to one
space.
Jesse
This is likely the fastest method if you precompile the regex.
2) You can use String.Split with
StringSplitOptions.RemoveEmptyEntries, and then String.Join the
result.
3) You can use Mark's suggestion.
But change it a bit. Contains will, buy default, start to look all the way
from the front each time. This is ok for small strings, but when going through
a large input (I usually test such things by copy pasting the whole contents
of "The Lord of the Rings" into a textbox), you're in trouble.
You already know where the last occurance of two spaces was found, so you
can remove those:
string tmp = userInput;
for (int i=0; i<tmp.Length, i++)
{
i= tmp.IndexOf(" ", i); // find the position of the two spaces, and
start looking at i, set i to the last position found
if (i == -1) { break; }
tmp = tmp.Remove(i, 1) // remove the first space
}
string result = tmp;
this will only pass the whole strign once.
It's probably even faster when doing this by loading the string into a stringbuilder
and work from there:
StringBuilder tmp = new StringBuilder(userInput);
for (int i = 0; i < tmp.Length -1 ; i++)
{
if (tmp.Chars
== ' ' && tmp.Chars[i+1] == ' ') // find the position
of the two spaces, at the current position
{
tmp.Remove(i, 1); // Remove the first space if you've found it
i = i--; // continue at the current position.
}
}
string result = tmp.ToString();
My guess is that the last options will be the fastest. The problem with these
string and stringbuilder options is that you'll need to make multiple passes
if you also want to remove multiple tabs, or other (whitespace) characters.
If you want that, look at the IndexOfAny function for the string based solution,
extend your if statement to go though multiple comparisons for the StringBuilder
option, or just use the regex above, which is getting more and more maintainable
compared to the other options
.