Javascript string object Extension Method

Source: Internet
Author: User
/*  * Append a string to the end of the string *  */  String. Prototype. append = Function  (STR ){  Return   This  . Concat (STR );}  /*  * Delete the characters at the specified index location. If the index is invalid, no characters will be deleted *  */  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 range * */  String. Prototype. deletestring = Function  (START, end ){  If (START = End ){  Return   This  . Deletecharat (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 );}}  /*  * Check whether 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  ;}}  /*  * You can use = to compare the two strings if they are equal *  */  String. Prototype. Equal =Function  (STR ){  If ( This . Length! = Str. Length ){  Return   False  ;}  Else  {  For ( VaR I = 0; I < This . Length; I ++ ){  If ( This . Charat (I )! = Str. charat (I )){  Return   False  ;}}  Return   True  ;}}  /*  * Compare the two strings to see if they are equal, Case Insensitive *  */  String. Prototype. equalignorecase = Function  (STR ){ VaR Temp1 = This  . Tolowercase ();  VaR Temp2 = Str. tolowercase ();  Return  Temp1.equal (temp2 );}  /*  * Insert the specified string to the end of the specified position. If the index is invalid, it is directly appended to the end of the string *  */  String. Prototype. insert = Function  (Ofset, substr ){  If (Ofset <0 | ofset> =This . Length-1 ){  Return   This  . Append (substr );}  Return   This . Substring (0, ofset + 1) + substr + This . Substring (ofset + 1 );}  /*  * Determine whether a string is a numeric string *  */  String. Prototype. isallnumber = Function (){  For ( VaR I = 0; I < This . Length; I ++ ){  If ( This . Charat (I) <'0' | This . Charat (I)> '9' ){  Return   False  ;}}  Return   True ;}  /*  * Sort strings 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 ;}  /*  * Set the character at the specified position to another character or string. If the index is invalid, the system will directly return the result without 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 );}  /*  * Check whether the string starts with substr *  */  String. Prototype. startwith = Function  (Substr ){  If (Substr. length> This  . Length ){ Return   False  ;}  Return ( This . Indexof (substr) = 0 )? True : False  ;}  /*  * Calculate the length. Each Chinese Character occupies two lengths, and each English character 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 spaces at the beginning and end *  */  String. Prototype. Trim = Function  (){  Return   This . Replace (/(^ \ s *) | (\ s * $)/g ,"");}  /*  * Test whether it is a 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  ;}  /*  * Test whether it is an integer *  */  String. Prototype. isint = Function  (){  If ( This = "Nan" ) Return   False  ;  Return   This = Parseint ( This  ). Tostring ();}  /*  * Obtain n identical strings *  */  String. Prototype. Repeat = Function  (Num ){  VaR Tmparr = []; For ( VaR I = 0; I <num; I ++) tmparr. Push ( This  );  Return Tmparr. Join ("" );}  /*  * Merge multiple blank spaces into one blank space *  */  String. Prototype. resetblank = Function  (){  Return   This . Replace (/S +/g ,"");}  /*  * Remove blank space on the left *  */  String. Prototype. ltrim = Function  (){  Return   This . Replace (/^ s +/g ,"" );}  /*  * Remove white space on the right *  */  String. Prototype. rtrim = Function (){  Return   This . Replace (/S + $/g ,"" );}  /*  * Remove blank spaces on both sides *  */  String. Prototype. Trim = Function  (){  Return   This . Replace (/(^ s +) | (S + $)/g ,"" );}  /*  * Reserved number * */  String. Prototype. getnum = Function  (){  Return   This . Replace (/[^ d]/g ,"" );}  /*  * Keep letters *  */  String. Prototype. geten = Function  (){  Return   This . Replace (/[^ A-Za-Z]/g ,"");}  /*  * Retain Chinese characters *  */  String. Prototype. getcn = Function  (){  Return   This . Replace (/[^ u4e00-u9fa5uf900-ufa2d]/g ,"" );}  /*  * Get the byte length *  */  String. Prototype. getreallength = Function (){  Return   This . Replace (/[^ x00-xff]/g ,"--" ). Length ;}  /*  * Truncates a string of the specified length from the left *  */  String. Prototype. Left = Function  (N ){  Return   This . Slice (0 , N );}  /*  * Extract a string of the specified length from the right * */  String. Prototype. Right = Function  (N ){  Return   This . Slice ( This . Length- N );} 

 

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.