views:

41

answers:

2

how do i trim using this

 $('#type_of_station').text()

the result is

>>> $('#type_of_station').text()
"pizza delivery system "

As you can see there is a space after the last word

+5  A: 

jQuery: str = jQuery.trim(str);

JavaScript: str = str.replace(/^\s*/, "").replace(/\s*$/, "");

greg
Please correct the jQuery answer.
sushil bharwani
What's wrong with it? `var str = " foo "; str = jQuery.trim(str); alert('"' + str + '"');` (shows "foo")
greg
The ECMAScript 5 standard outlines a [`trim()`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/Trim) function native to the `String` object. Currently, I believe, it's only available in JS >= 1.8.1 (meaning FireFox 3.5+). Hopefully others will follow suit eventually and `.trim()` will work on all modern browsers natively.
clarkf
@clarkf - Safari and Chrome have it as well. According to this page it's coming in IE9: http://msdn.microsoft.com/en-us/library/ff974378(VS.85).aspx Nice thing is that jQuery's `$.trim()` [defaults to the native implementation](http://github.com/jquery/jquery/blob/master/src/core.js#L630) when it's available.
patrick dw
+2  A: 

Use the JQuery.trim() function.

Suresh Kumar