Advertisement
sylviapsh

String Extensions JS - Doncho's

May 19th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function () {
  2.     if (!String.prototype.contains) {
  3.         String.prototype.contains = function (searchPattern) {
  4.             var patternLen = searchPattern.length;
  5.             for (var i = 0, length = this.length - patternLen; i < length; i++) {
  6.                 var isMatch = true;
  7.                 for (var j = 0; j < patternLen; j++) {
  8.                     if (searchPattern[j] !== this[i + j]) {
  9.                         isMatch = false;
  10.                         break;
  11.                     }
  12.                 }
  13.                 if (isMatch) {
  14.                     return true;
  15.                 }
  16.             }
  17.         }
  18.     }
  19.  
  20.     if (!String.prototype.toCharArray) {
  21.         String.prototype.toCharArray = function () {
  22.             return this.split("");
  23.         }
  24.     }
  25.  
  26.     if (!String.prototype.trimLeft) {
  27.         String.prototype.trimLeft = function () {
  28.             return this.replace(/^\s+/, "");
  29.         }
  30.     }
  31.     if (!String.prototype.trimRight) {
  32.         String.prototype.trimRight = function () {
  33.             return this.replace(/\s+$/, "");
  34.         }
  35.     }
  36.  
  37.     if (!String.prototype.trim) {
  38.         String.prototype.trim = function () {
  39.             return this.trimLeft().trimRight();
  40.         }
  41.     }
  42.  
  43.     if (!String.prototype.trimLeftChars) {
  44.         String.prototype.trimLeftChars = function (chars) {
  45.             var regEx = new RegExp("^[" + chars + "]+");
  46.             return this.replace(regEx, "");
  47.         }
  48.     }
  49.  
  50.     if (!String.prototype.trimRightChars) {
  51.         String.prototype.trimRightChars = function (chars) {
  52.  
  53.             var regEx = new RegExp("[" + chars + "]+$");
  54.             return this.replace(regEx, "");
  55.         }
  56.     }
  57.  
  58.     if (!String.prototype.trimChars) {
  59.         String.prototype.trimChars = function (chars) {
  60.             return this.trimLeftChars(chars).trimRightChars(chars);
  61.         }
  62.     }
  63.  
  64.     if (!String.prototype.htmlEscape) {
  65.         String.prototype.htmlEscape = function () {
  66.             return this.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, "&#39").replace(/ /g,"&nbsp;");
  67.         }
  68.     }
  69.     if (!String.prototype.padLeft) {
  70.         String.prototype.padLeft = function (count, char) {
  71.             char = char || " ";
  72.             if (char.length > 1) {
  73.                 return String(this);
  74.             }
  75.             if (this.length >= count) {
  76.                 return String(this);
  77.             }
  78.             var str = String(this);
  79.             for (var i = 0, thisLen = str.length; i < count - thisLen; i++) {
  80.                 str = char + str;
  81.             }
  82.             return str;
  83.         }
  84.     }
  85.  
  86.     if (!String.prototype.padRight) {
  87.         String.prototype.padRight = function (count, char) {
  88.             char = char || " ";
  89.             if (char.length > 1) {
  90.                 return String(this);
  91.             }
  92.             if (this.length >= count) {
  93.                 return String(this);
  94.             }
  95.             var str = String(this);
  96.             for (var i = 0, thisLen = this.length; i < count - thisLen; i++) {
  97.                 str += char;
  98.             }
  99.             return str;
  100.         }
  101.     }
  102. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement