JS removes Spaces before and after strings

ECMAScript 5 adds a trim() prototype method to strings that removes leading, trailing, and line terminators from strings. This method is very practical in expression processing.

Null characters include Spaces, tabs, page feeds, carriage returns, and line feeds.
The sample
The following code uses the trim() method to quickly remove the leading and trailing whitespace from the string.

var s = "    abc def   \r\n  ";
s = s.trim();
console.log("[" + s + "]");  //[abc def]
console.log(s.length);  //7

Leave a Comment