Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've read this question about javascript trim, with a regex answer.

Then I expect trim to remove the inner space between Hello and World.

function myFunction() {
    alert("Hello World ".trim());
}

EDITED

Why I expected that!?

Nonsense! Obviously trim doesn't remove inner spaces!, only leading and trailing ones, that's how trim works, then this was a very wrong question, my apologies.

share|improve this question

closed as too localized by Rene Pot, Juhana, mgraph, Burkhard, casperOne May 29 '12 at 21:16

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit your question.

7  
trim removes whitespace from the beginning and end of a string –  ManseUK May 29 '12 at 13:42
2  
Trim removes whitespace from before and after a string. Hello World is already "trimmed'. –  Rocket Hazmat May 29 '12 at 13:42
1  
@void trim remove spaces at the begin and at the end of a string - it doesn't remove spaces in the middle - and you should also improve your accept rate –  Fabrizio Calderan May 29 '12 at 13:43
4  
@void: That's not "trimming", that's a different problem. –  Rocket Hazmat May 29 '12 at 13:43
3  
@Rocket You are right, I would downvote myself if I could –  Hernán Eche May 29 '12 at 13:44

4 Answers 4

up vote 27 down vote accepted

For space-character removal use

"hello world".replace(" ", "");

for all white space use the suggestion by Rocket in the comments below!

share|improve this answer
22  
You might want to use .replace(/ /g, '') if there is more than once space. –  Rocket Hazmat May 29 '12 at 13:44
    
@Rocket, that is absolutely true, just havent updated answer yet! Thanks! :) –  limelights May 29 '12 at 13:44
1  
The space character is not exactly "all whitespace"... –  Juhana May 29 '12 at 13:44
11  
@Juhana: If you want to remove other whitespace too then do: .replace(/\s/g, ''). –  Rocket Hazmat May 29 '12 at 13:45
5  
@RocketHazmat yes! the correct answer! though this might be slightly more efficient: str.replace(/\s+/g, '') –  jackocnr Jan 8 '14 at 1:47

Probably because you forgot to implement the solution in the accepted answer. That's the code that makes trim() work.

update

This answer only applies to older browsers. Newer browsers apparently support trim() natively.

share|improve this answer
2  
Not necessarily, some browsers have it built in. :-P –  Rocket Hazmat May 29 '12 at 13:42
1  
Its present in most modern browers now -> kangax.github.com/es5-compat-table –  ManseUK May 29 '12 at 13:42
1  
-1. Sorry, but this is really wrong and not an answer, either. –  minitech May 29 '12 at 13:44
    
I had no idea they've implemented this. IT's about time. –  John Conde May 29 '12 at 13:45

try this:

alert("Hello World    ".trim());
share|improve this answer
2  
NO! See all the comments about trim on the question! –  jackocnr Jan 8 '14 at 1:45

You need to extend 'String' as mentioned in http://stackoverflow.com/a/498995/486348. After that you can use trim function in javascript.

share|improve this answer

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