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

xRo*_*bot 14 javascript

可能重复:
如何在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"
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点 ?

pap*_*tis 17

来自jQuery修剪是你正在寻找的.

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

或者简单的javascript:

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