﻿

String.prototype.htmlChars = function () {
    var str = ((this.replace('"', '&amp;')).replace('"', '"'));
    return (str.replace('<', '&lt;')).replace('>', '&gt;');
}
String.prototype.trim = function () { return this.replace(/\s/g, '');  }

String.prototype.ltrim = function () { return this.replace(/^s*/g, ""); }


String.prototype.rtrim = function () { return this.replace(/s*$/g, ""); }

String.prototype.stripTags = function () {
    var str = this;
    var pos1 = str.indexOf('<');

    if (pos1 == -1) return str;
    else {
        var pos2 = str.indexOf('>', pos1);
        if (pos2 == -1) return str;
        return (str.substr(0, pos1) + str.substr(pos2+1)).stripTags();
    }
}

String.prototype.ipos = function (needle, offset) {
    var offset = (typeof offset == "number")?offset:0;
    return str.toLowerCase().indexOf(needle.toLowerCase(), offset);
}

String.prototype.ripos = function (needle, offset) {
    var offset = (typeof offset == "number")?offset:0;
    return str.toLowerCase().lastIndexOf(needle.toLowerCase(), offset);
}

String.prototype.toArray = function () {
    var len = this.length;
    var arr = new Array;
    for (var i=0; i<len; i++) arr[i] = this.charAt(i);
    return arr;
}

String.prototype.bytes = function() {
    var bytes = 0;

    for(var i=0; i < this.length; i++) {
        bytes += (this.charCodeAt(i) > 128)?1:2;
    }
    return bytes;
}

