2012-05-30 44 views
7

可能重複:
How do I trim a string in javascript?如何從字符串中刪除最後和第一個空格?

我只需要刪除最後,從一個普通字符串的第一個空格。

例子:

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" 

我怎麼能這樣做?

+2

而http://stackoverflow.com/questions/196925/what-is-the-best-way-to-trim-in- javascript –

+0

這就是所謂的修剪。請參閱http://stackoverflow.com/questions/498970/how-do-i-trim-a-string-in-javascript/8522376#8522376或http://stackoverflow.com/questions/498970/how-do-i -trim-A-字符串中的JavaScript – sgowd

回答

10

trim from jQuery是你在找什麼。

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

或者普通的JavaScript:

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

相關問題