Let's say I have a string such as:
"Hello how are you doing?"
I would like a function that turns multiple spaces into one space.
So I would get:
"Hello how are you doing?"
I know I could use regex or call
string s = "Hello how are you doing?".replace(" "," ");
But I would have to call it multiple times to make sure all sequential whitespaces are replaced with only one.
Is there already a built in method for this?
Smallest solution:
var regExp=/\s+/g, newString=oldString.replace(regExp,' ');
This question isn't as simple as other posters have made it out to be (and as I originally believed it to be) - because the question isn't quite precise as it needs to be.
There's a difference between "space" and "whitespace". If you only mean spaces, then you should use a regex of
" {2,}"
. If you mean any whitespace, that's a different matter. Should all whitespace be converted to spaces? What should happen to space at the start and end?For the benchmark below, I've assumed that you only care about spaces, and you don't want to do anything to single spaces, even at the start and end.
Note that correctness is almost always more important than performance. The fact that the Split/Join solution removes any leading/trailing whitespace (even just single spaces) is incorrect as far as your specified requirements (which may be incomplete, of course).
The benchmark uses MiniBench.
A few test runs:
Here the first number is the number of iterations, the second is the time taken, and the third is a scaled score with 1.0 being the best.
That shows that in at least some cases (including this one) a regular expression can outperform the Split/Join solution, sometimes by a very significant margin.
However, if you change to an "all whitespace" requirement, then Split/Join does appear to win. As is so often the case, the devil is in the detail...
I'm sharing what I use, because it appears I've come up with something different. I've been using this for a while and it is fast enough for me. I'm not sure how it stacks up against the others. I uses it in a delimited file writer and run large datatables one field at a time through it.
As already pointed out, this is easily done by a regular expression. I'll just add that you might want to add a .trim() to that to get rid of leading/trailing whitespace.
There is no way built in to do this. You can try this:
This will remove leading and trailing whitespce as well as collapse any internal whitespace to a single whitespace character. If you really only want to collapse spaces, then the solutions using a regular expression are better; otherwise this solution is better. (See the analysis done by Jon Skeet.)