Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have strings with extra whitespaces, each time there's more than only one whitespace I'd like it be only one.

Anyone? I tried searching google, but nothing worked for me.

Thanks

share|improve this question
1  
remove duplicate whitespace from the end/beginning, or anywhere in the text? –  ninjagecko May 28 '11 at 17:24

7 Answers

up vote 166 down vote accepted

Something like this:

s.replace(/\s+/g, ' ');
share|improve this answer
7  
Surely the 2nd of the two arguments to replace() should be an empty string: s.replace(/\s+/g, ''); Since that's the new value you wish to embed in place of whitespace. The code as it stands actually puts whitespace into the resultant string. –  MeMory LEAk99 Dec 27 '12 at 17:13
20  
@Me Please read the question carefully: "each time there's more than only one whitespace I'd like it be only one". –  bjornd Dec 29 '12 at 9:45
 
So how does that actually work? –  Ciaran Gallagher Mar 3 at 23:20
3  
Isn't it obvious? it replaces more than 1 whitespace char with 1 whitespace char. (the desired result) –  Wardy Mar 22 at 16:05
 
@CiaranG It's a regular expresion –  pomeroy Jun 13 at 15:35

You can augment String to implement these behaviors as methods, as in:

String.prototype.killWhiteSpace = function() {
    return this.replace(/\s/g, '');
};

String.prototype.reduceWhiteSpace = function() {
    return this.replace(/\s+/g, ' ');
};

This now enables you to use the following elegant forms to produce the strings you want:

"Get rid of my whitespaces.".killWhiteSpace();
"Get rid of my extra        whitespaces".reduceWhiteSpace();
share|improve this answer
7  
Augmenting prototype of the standard object is a really controversial pattern. I wouldn't recommend it for such a basic question. –  bjornd Nov 2 '12 at 9:06

using a regular expression with the replace function does the trick:

string.replace(/\s/g, "")
share|improve this answer

I presume you're looking to strip spaces from the beginning and/or end of the string (rather than removing all spaces?

If that's the case, you'll need a regex like this:

mystring = mystring.replace(/(^\s+|\s+$)/g,' ');

This will remove all spaces from the beginning or end of the string. If you only want to trim spaces from the end, then the regex would look like this instead:

mystring = mystring.replace(/\s+$/g,' ');

Hope that helps.

share|improve this answer
3  
I like the simplicity of your regex, @Spudley, but won't your code replace runs of whitespace at the beginning and end with a single space character? I thought the goal was to remove the whitespace from the ends entirely, in which case the replacement string should be '' instead of ' '. –  Randall Cook Oct 16 '12 at 18:44

jQuery.trim() works well.

http://api.jquery.com/jQuery.trim/

share|improve this answer
8  
Just remember that this doesn't remove all whitespace. Quoting the docs: "Remove the whitespace from the beginning and end of a string." –  Jonik Aug 22 '12 at 10:52

var x = " Test Test Test ".split(" ").join(""); alert(x);

share|improve this answer
4  
It works but it is not smart coding. This code is slower than a RegEx. I wonder if Split() does not use RegEx to split a string ! –  Farandole Oct 31 '12 at 19:40

I know I should not necromancy on a subject, but given the details of the question, I usually expand it to mean:

  • I want to replace multiple occurences of whitespace inside the string with a single space
  • ...and... I do not want whitespaces in the beginnin or end of the string (trim)

For this, I use code like this (the parenthesis on the first regexp are there just in order to make the code a bit more readable ... regexps can be a pain unless you are familiar with them):

s = s.replace(/^(\s*)|(\s*)$/g, '').replace(/\s+/g, ' ');

The reason this works is that the methods on String-object return a string object on which you can invoke another method (just like jQuery & some other libraries). Much more compact way to code if you want to execute multiple methods on a single object in succession.

share|improve this answer
 
+1 for the "ready for use" example .. I was going to post it my self –  d.raev Jul 31 at 8:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.