Re: Multiple blanks
- From: Jesse Houwing <jesse.houwing@xxxxxxxxxxxxxxxx>
- Date: Tue, 21 Apr 2009 10:15:50 +0000 (UTC)
Hello Pavel,
On Apr 20, 4:00 pm, "tshad" <t...@xxxxxxxx> wrote:
Is there an easy way to take multiple blanks out of string?1) You can use Regex.Replace:
I have a string that a user puts in but he may put multiple blanks in
and I would like to change 2 or 3 or 4... blanks to 1.
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[i] == ' ' && 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 ;).
--
Jesse Houwing
jesse.houwing at sogeti.nl
.
- Follow-Ups:
- Re: Multiple blanks
- From: Pavel Minaev
- Re: Multiple blanks
- References:
- Re: Multiple blanks
- From: Pavel Minaev
- Re: Multiple blanks
- Prev by Date: Difference between CodeDOM and CompilerServices ?
- Next by Date: How do I get the current cursor position in a TextBox
- Previous by thread: Re: Multiple blanks
- Next by thread: Re: Multiple blanks
- Index(es):
Relevant Pages
|