2012-05-30 16 views
7

Mögliche Duplizieren:
How do I trim a string in javascript?Wie entferne ich letzte und erste Leerzeichen von einer Zeichenkette?

Ich brauche nur die letzten und die ersten Räume von einem generischen Zeichenfolge zu entfernen.

Beispiel:

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" 

Wie kann ich das tun?

+2

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

+0

Das heißt Trimmen genannt. Siehe http://stackoverflow.com/questions/498970/how-doi-i-trim-a-string-in-javascript/8522376#8522376 oder http://stackoverflow.com/questions/498970/how-do-i -trim-a-string-in-Javascript – sgowd

Antwort

10

Die trim from jQuery ist, was Sie suchen.

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

Oder schlicht javascript:

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

Verwandte Themen