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("")
1

2 Answers 2

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!

Sign up to request clarification or add additional context in comments.

4 Comments

oh wow. how have i never used that??? thanks :)
You should point out that this is not supported in IE < 9
damn, we need to make it work in IE8+. is there a function that will work in IE8? thanks!
found it... $.trim(str) in jquery.
2

JQuery (Seems you use that):

$.trim(str)

Works in all browsers

Please note that

Str.trim() 

Does not work in IE8

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.