Modifying the String class in Javascript
Tuesday, September 11th, 2007Ivan Uzunov has posted his top 10 modifications to the String prototype in Javascript. While all modifications to the prototype of one of Javascript’s built-in classes is fraught with complications, some of these methods seem very useful. I wish they were in the core of the Javascript language. To split an array into a string is something I do all the time in PHP, so I wish Javascript had something like this:
This extension splits the string by given separator and returns an array with trimmed items. It uses the trim() extension above:
String.prototype.splitrim = function(t){ return this.trim().split(new RegExp(’\\s*’+t+’\\s*’)) }//test splitrim
test = ‘ testing , splitrim ‘;
var arr = test.splitrim(’,’);
document.write (’”’ + arr[0] + ‘”’);
document.write (’”’ + arr[1] + ‘”’);