JavaScript String Object Extension method

Source: Internet
Author: User

/** appends the string to the end of the string **/string.prototype.append = function (str) {return this.concat (str);} /** Delete the character at the specified index, invalid index will not remove any characters **/string.prototype.deletecharat = function (index) {if (Index < 0 | | Index >= THIS    . length) {return this.valueof ();    } else if (index = = 0) {return this.substring (1, this.length);    } else if (index = = this.length-1) {return this.substring (0, this.length-1);    } else {return this.substring (0, index) + this.substring (index + 1); }}/** Delete the string of the specified index interval **/string.prototype.deletestring = function (start, end) {if (start = = end) {return this.de    Letecharat (start);            } else {if (Start > End) {var temp = start;            start = end;        end = temp;        } if (Start < 0) {start = 0;        } if (end > this.length-1) {end = This.length-1; } return this.substring (0, start) + this.substring (end +1, this.length);   }}/** checks if the string ends with subStr **/string.prototype.endwith = function (subStr) {if (Substr.length > This.length) {    return false;    } else {return (This.lastindexof (subStr) = = (This.length-substr.length))? True:false; }}/** compares two strings for equality, or you can compare them directly with = = **/string.prototype.equal = function (str) {if (this.length! = str.length) {R    Eturn false;                } else {for (var i = 0; i < this.length; i++) {if (This.charat (i)! = Str.charat (i)) {            return false;    }} return true;    }}/** compares two strings for equality, case insensitive **/string.prototype.equalignorecase = function (str) {var temp1 = this.tolowercase ();    var temp2 = Str.tolowercase (); Return temp1.equal (TEMP2);} /** inserts the specified string after the specified position, an invalid index is appended directly to the end of the string **/string.prototype.insert = function (Ofset, SUBSTR) {if (Ofset < 0 | | ofse    T >= this.length-1) {return this.append (SUBSTR); } return this.substring (0, Ofset + 1) + SubStr + This.substriNg (Ofset + 1);} /** determines whether a string is a number string **/string.prototype.isallnumber = function () {for (var i = 0; i < this.length; i++) {if (th Is.charat (i) < ' 0 ' | |        This.charat (i) > ' 9 ') {return false; }} return true;    /** the string in reverse order **/string.prototype.reserve = function () {var temp = "";    for (var i = this.length-1; I >= 0; i--) {temp = Temp.concat (This.charat (i)); } return temp;}  /** sets the character of the specified position to another specified character or string. Invalid index will return directly without doing any processing **/string.prototype.setcharat = function (index, SUBSTR) {if (Index < 0 ||    Index > This.length-1) {return this.valueof (); } return this.substring (0, index) + subStr + this.substring (index+1);} /** checks if the string starts with subStr **/string.prototype.startwith = function (subStr) {if (Substr.length > This.length) {r    Eturn false; } return (This.indexof (subStr) = = 0)? True:false;}    /** calculates the length, each Chinese character occupies two length, the English character each occupies one length **/string.prototype.charlength = function () {var temp = 0; for (var i = 0; i < this.length;        i++) {if (This.charcodeat (i) > 255) {temp + = 2;        } else {temp + = 1; }} return temp;} String.prototype.charLengthReg = function () {return this.replace (/[^\x00-\xff]/g, "* *"). Length; /** Remove the trailing space **/string.prototype.trim = function () {return this.replace (/(^\s*) | ( \s*$)/g, "");}    /** test whether the number **/string.prototype.isnumeric = function () {var tmpfloat = parsefloat (this);    if (IsNaN (Tmpfloat)) return false;    var Tmplen = this.length-tmpfloat.tostring (). length; return tmpfloat + "0". Repeat (tmplen) = = this;}    /** whether the test is an integer **/String.prototype.isInt = function () {if (this = = "NaN") return false; return this = = parseint (this). toString ();}    /** gets n the same string **/string.prototype.repeat = function (num) {var tmparr = [];    for (var i = 0; i < num; i++) Tmparr.push (this); Return Tmparr.join ("");} /** merges multiple blanks for a blank **/String.prototype.resetBlank = function () {return this.Replace (/s+/g, "");} /** Remove left blank **/String.prototype.LTrim = function () {return this.replace (/^s+/g, "");} /** Remove the right blank **/String.prototype.RTrim = function () {return this.replace (/s+$/g, "");} /** remove both sides blank **/String.prototype.trim = function () {return this.replace (/(^s+) | ( s+$)/g, "");} /** reserved number **/String.prototype.getNum = function () {return this.replace (/[^d]/g, "");} /** reserved letters **/String.prototype.getEn = function () {return this.replace (/[^a-za-z]/g, "");} /** reserved Chinese **/String.prototype.getCn = function () {return this.replace (/[^u4e00-u9fa5uf900-ufa2d]/g, "");} /** gets the byte length **/String.prototype.getRealLength = function () {return this.replace (/[^x00-xff]/g, "--"). Length; /** A string of the specified length from the left **/String.prototype.left = function (n) {return This.slice (0, n);} /** A string of the specified length from the right **/String.prototype.right = function (n) {return this.slice (this.length-n);}

JavaScript String Object Extension method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.