Re: Multiple blanks



Hello Pavel,

On Apr 20, 4:00 pm, "tshad" <t...@xxxxxxxx> wrote:

Is there an easy way to take multiple blanks out of string?

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.

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[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


.



Relevant Pages

  • Re: String concatenation performance
    ... > into one string for use over multiple lines of code. ... Running the sample many times via an internal loop. ... in a container and that container is using system resources along with the ...
    (comp.lang.javascript)
  • Re: issue with runing Select query with condition using code
    ... You can then apply it as the Filter of a form, or the WhereCondition of OpenReport, or build the whole SQL string and apply it to the SQL property of a QueryDef. ... Splitmight be useful for parsing multiple elements in a text box into an array. ... Be sure to use the correct delimiter around the values, i.e. " for strings, # for dates, and no delimiter for numeric values. ... I'm trying to use the DoCmd.open query statement to run a Select query. ...
    (microsoft.public.access.modulesdaovba)
  • compression of strings into curly braces
    ... for host in host; do rsh $host uptime; done ... to expand a string into a set of related members. ... compression algorithm. ... run-length encoding, as plain text, contiguous stretches of multiple ...
    (comp.programming)
  • Re: Introducing Python to others
    ... A simple class example and a class example showing multiple ... Simple string manipulation example ... Lists, dictionaries and sets ... Advanced Python features: ...
    (comp.lang.python)
  • Re: What am I doing wrong?
    ... This will force the variant type so that it is either NaN or one of the ... since it always does that I only use parseInt() if I need the filed to ... >>I always will multiple a string by one to force it to either become ...
    (microsoft.public.scripting.jscript)