2012-05-30 2 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-string-in-javascript – sgowd

답변

10

trim from jQuery이 당신이 찾고있는 것입니다.

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

또는 일반 자바 스크립트 :

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

관련 문제