如何从字符串中删除最后和第一个空格?

时间:2012-05-30 07:34:52

标签: javascript

  

可能重复:
  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"

我该怎么做?

1 个答案:

答案 0 :(得分:17)

trim from jQuery正是您要找的。

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

或普通的javascript:

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