16

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

I need to remove only the last and the first spaces from a generic string.

Example:

var str = " hello world "; // become "hello world"
var str = "ehi, do you come from ? "; // become "ehi, do you come from ?"
var str = "I am from Cina"; // remain "I am from Cina"

How can I do that ?

Community
  • 1
  • 1
xRobot
  • 25,579
  • 69
  • 184
  • 304
  • 2
    And http://stackoverflow.com/questions/196925/what-is-the-best-way-to-trim-in-javascript – Andrew Leach May 30 '12 at 07:37
  • That's called trimming. Refer http://stackoverflow.com/questions/498970/how-do-i-trim-a-string-in-javascript/8522376#8522376 or http://stackoverflow.com/questions/498970/how-do-i-trim-a-string-in-javascript – sgowd May 30 '12 at 07:37

1 Answers1

28

The trim from jQuery is what you are looking for.

$.trim(' string with spaces at the ends ');

Or plain javascript:

' string with spaces at the ends '.trim()

papaiatis
  • 4,231
  • 4
  • 26
  • 38