Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Monday, February 1, 2016

String strip() for JavaScript?

String strip() for JavaScript?


What's a clean and efficient JavaScript implementation to strip leading and trailing spaces from a string?

For example:

" dog"

"dog "

" dog "

" dog "

all get turned into

"dog"

Answer by David Andres for String strip() for JavaScript?


Use this:

if(typeof(String.prototype.trim) === "undefined")  {      String.prototype.trim = function()       {          return String(this).replace(/^\s+|\s+$/g, '');      };  }  

The trim function will now be available as a first-class function on your strings. For example:

" dog".trim() === "dog" //true  

EDIT: Took J-P's suggestion to combine the regex patterns into one. Also added the global modifier per Christoph's suggestion.

Took Matthew Crumley's idea about sniffing on the trim function prior to recreating it. This is done in case the version of JavaScript used on the client is more recent and therefore has its own, native trim function.

Answer by Mic for String strip() for JavaScript?


Here's the function I use.

function trim(s){     return ( s || '' ).replace( /^\s+|\s+$/g, '' );   }  

Answer by Gumbo for String strip() for JavaScript?


Steven Levithan once wrote about how to implement a Faster JavaScript Trim. It?s definitely worth a look.

Answer by Eric for String strip() for JavaScript?


If, rather than writing new code to trim a string, you're looking at existing code that calls "strip()" and wondering why it isn't working, you might want to check whether it attempts to include something like the prototypejs framework, and make sure it's actually getting loaded.
That framework adds a strip function to all String objects, but if e.g. you upgraded it and your web pages are still referring to the old .js file it'll of course not work.

Answer by user638373 for String strip() for JavaScript?


For jquery users, how about $.trim(s)

Answer by jpatokal for String strip() for JavaScript?


Gumbo already noted this in a comment, but this bears repeating as an answer: the trim() method was added in JavaScript 1.8.1 and is supported by all modern browsers (Firefox 3.5+, IE 9, Chrome 10, Safari 5.x), although IE 8 and older do not support it. Usage is simple:

 "  foo\n\t  ".trim() => "foo"  

See also:

Answer by Filip Dupanovi for String strip() for JavaScript?


If you're already using jQuery, then you may want to have a look at jQuery.trim() which is already provided with jQuery.

Answer by Bardi Harborow for String strip() for JavaScript?


A better polyfill from the MDN that supports removal of BOM and NBSP:

if (!String.prototype.trim) {    (function(){        // Make sure we trim BOM and NBSP      var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;      String.prototype.trim = function () {        return this.replace(rtrim, "");      }    })();  }  

Also bear in mind that modifying the prototypes of builtins slows down runtime optimizations, so it may be better to define myTrimFunction(string) rather than the above approach. Then again if the browser does have String.trim than that WOULD be faster. Decisions, decisions!


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.