1

Possible Duplicate:
How do I trim a string in JavaScript?

So....

"   steve   monkey   "

would become "steve monkey"

and...

"   cheese charlie face   "

would become "chesse charlie face"

so basically take out all space characters that occur before the first non-space character, and also remove all space characters after the last non-space character, but don't remove any spaces in between. thanks!!!

right now i am just removing all spaces like below, but i learned we need to keep spaces in the middle... :(

$newPassword.val().split(" ").join("")
Community
  • 1
  • 1
Steven
  • 406
  • 4
  • 11

2 Answers2

4

Use trim():

var testString = "                 hello                   wold        ";
alert("The text:"+testString.trim()+"End here");

EDIT: As noted in the comments and in the other answer, this doesn't work in IE < 9. The jquery option does work on all browsers:

$.trim(testString);

Thanks to Joseph and mplungjan for clearing that!

Juan Techera
  • 1,192
  • 2
  • 14
  • 25
2

JQuery (Seems you use that):

$.trim(str)

Works in all browsers

Please note that

Str.trim() 

Does not work in IE8

mplungjan
  • 169,008
  • 28
  • 173
  • 236