Properties and methods of the Javascript string class

Source: Internet
Author: User

String class Attribute and Method Anchor ()               Create an instance of the <a> tag, Set its Name property to the string that is passed to this method big ()                       Convert a string to an instance of the <big> tag blink ()                     Convert a string to an instance of the <blink> tag Bold ()                    Converts a string to an instance of the <bold> label CharAt ()                 returns the character at the index passed to this method concat ()              The    connection is passed two strings to return a new string. Fixed ()                   Converts a character <tt> a fixed pitch font label to an instance of FontColor ()             set the Color property of a <font> tag instance fontsize ()               set a <font> tag instance's Size property fromCharCode ()    returns the iso-latin-1 numeric character passed to this party indexOf () Returns the index of the first occurrence of the string passed to this method in an instance of an Sting object italics ()                    converts a string to an instance of a <i> tag lastIndexOf () returns the index of the string passed to this method in an instance of an Sting object last occurrence Link ()                        converts a string to an instance of the <a> tag and uses the Herf property set by the URL passed to this method match ()                   returns an array that contains the matches found for the regular expression that is passed to this method. Replace ()                Performs a lookup and substitution with its strin instance using the rule expression and substitution string that are passed to this method. Search ()                returns the word that is passed to this methodMatches the index position found in the string. If the character distance is not found, return-1 Slice ()                     returns the string that is passed between the start and end indexes of this method. If a negative value is passed, the index is referenced as the tail of the passed string. Small ()                   Swap the string as an instance of the <small> tag split () returns a string that is split into segments, defined by the string and instance restrictions passed to this method Strike ()                        Convert a string to an instance of the <strike> label Sub () Converts a string to an instance of the <sub> label Substr ()                  returns a string from the indexed position that contains a certain number of characters to return. If a negative value is passed, the index is referenced as the end of the string being passed as a reference substring ()             Returns the character that is passed between the start and End Indexes sup ()                       Convert a string to an instance of the <sup> tag tolowercase ()      Converts all characters in a string to lowercase tosource ()              Returns the passed character as a string representation of a String Object toString ()                 returns the passed character as a string type toUpperCase ()       converts all characters in a string to uppercase length ()                   returns the length of the string prototype ()             provides a program with the ability to add properties to a String object instance

Extension of the String class: String.prototype.tochararray=function () {         return This.split ("");} Reverse String.prototype.Reverse = function () {return this.split (""). Reverse (). Join (""); Contains the specified character String.prototype.IsContains = function (str) {return (This.indexof (str) >-1);}//Determines whether it is an empty string.prototype. Isempty=function () {return this== "";}//Determine if 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". Getsame (Tmplen) ==this; }//Determines whether it is an integer String.prototype.IsInt = function () {if (this = = "NaN") return false, return this = = parseint (this). ToString (); }//merge multiple blanks for a blank String.prototype.resetBlank = function () {return this.replace (/s+/g, "");}//Remove left margin String.prototype.LTrim = function () {return this.replace (/^s+/g, "");}//Remove 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 letter String.proto Type.geten = function () {    return this.replace (/[^a-za-z]/g, "");}//reserved Chinese String.prototype.getCn = Func tion () {    return this.replace (/[^u4e00-u9fa5uf900-ufa2d]/g, "");}//Get byte length String.prototype.bytelength=function () {return this.replace (/[^\x00-\xff]/g, "AA"). length;//intercept a specified length of string from the left String.prototype.left = function (n) {    return This.slice (0,n)}//intercept a specified length of string from right String.prototype.right = function (n) {    return this.slice (this.length-n);}//HTML-encoded String.prototype.HTMLEncode = function () {    var re = this;     var q1 = [/x26/g,/x3c/g,/x3e/g,/x20/g];     var q2 = ["&", "<", ">", ""];     for (var i=0;i<q1.length;i++)     re = Re.replace (Q1[i],q2[i]);     RETurn re; }//Get Unicode String.prototype.Unicode = function () {var tmparr=[]; for (var i=0; i<this.length; i++) Tmparr.push ("&amp ; # "+ This.charcodeat (i) +"; "); Return Tmparr.join ("");} Specifies the position of the insertion string string.prototype.insert=function (index,str) {return this.substring (0,index) +str+this.substr (index);}/ /copy String string.prototype.copy=function () {if (IE) window.clipboardData.setData ("Text", this.tostring ());}// Append formatted string string.prototype.appendformat=function (str) {var arg=arguments; Str=str.replace (/\{(\d+) \}/g,function (a , b,c) {   if (Arg[parseint (b) +1]) return Arg[parseint (b) +1];    else return A;}); return this+str; }

Creates a String object, syntax: New string (StringValue), which converts the argument to a string and acts as a string object. In fact, any string constant is a string object that can be used directly as an object, and the difference between creating an object using the new string () is that the return value of typeof is different, one is "stirng" and the other is "object".
String.IndexOf (searchstring,position)----start with the position (optional) position, searching for where the first searchsting in the string appears and returning. For example: "Hello,jack". IndexOf ("Hello") will return 0; "Abcabc". IndexOf ("A", 1) will return 3;
String.LastIndexOf (searchstring,position)--------start with the position (optional) position, searching for where the last searchstring in the string appears and returning. such as: "Abcabc". LastIndexOf ("B") will return 4
String.charat (POS)-------returns the character in the string where Pos is located. For example: "ABC". CHARAT (1) return "B"
Stirng.charcodeat (POS)------Returns the character code in the string where Pos is located. For ASCII characters, this returns its ASCII code. For example: "ABC". charCodeAt (0) returns 97, which represents the ASCII code of the character "a".
String.slice (Start,end)--------returns a substring in the string starting at start and ending at end (not including end)
String.Split (Separator,linmit)------cut strings into multiple substrings with separator as delimiters and return them as an array. Linmit (optional) represents the maximum length of the array, and the excess portion is discarded. The separator delimiter is not included in any substrings, and if Sepatator is an empty string, an array of character sequences in the string is returned. If the split method does not have any arguments, it returns an array that contains only the string itself and only one element.
String.Split (Separator,linmit)-------For example: "A1,b1,c1". Split (",") will return ["A1", "B1", "C1"];
"A,b,c". Split (",", 2) will return ["A", "B"];
"A,b,c". Split ("") will return ["a", ",", "B", ",", "C"];
"Ab,c". Split () returns the ["Ab,c"] string.substr (start,length)--------returns a substring with a length from start to start in the string. For example: "ABCDEFG". substr (1,3) will return "BCD";
String.substring (start,end)------Returns a substring where the starting position in the string is start and the ending position is end (including end). The only difference between this method and the slice method is the character that contains the end position.
Replacing and matching strings
(1) Replace (Searchvalue,replacevalue) method This method replaces the first occurrence of the Searchvalue substring in a string with Replacevalue and returns a new string. The original string is not affected.
For example: Var str1= "AAAA";
var str2=str1.replace ("A", "B");
alert (str2);//Output "BAAA" alert (str1);//Output "AAAA"
As you can see in the code above, you can only replace one instance with the Replace function. If you are replacing multiple instances, you need to use regular expressions, such as str.replace (/a/g, "B") to replace "AAAA" with "bbbb".
(2) match (Reexp) method
Searches the string for all substrings that match the regexp regular expression, returning them as an array. You can also determine whether a string matches the regular expression represented by the regexp by using the conversion rules of the object type to the Boolean type.
For example: Var strinput=prompt ("Please enter a number:", 0);
while (!strinput.match (/\d+/)) {strinput=prompt ("Please enter a number:", 0);}
(3) Search (REGEXP) method
Searches for the first substring of a matching regexp regular expression from a string, returning its index position. For example: Var str= "AABCABCABC";
Alert (Str.search (/abc/g));//display "1"
(4) Case conversion of a String object
var str= "abc";
Str.tolowercase ()//Convert lowercase
Str.touppercase ()//convert capitalization
(5) connection of a String object
var str= "abc";
var str2=str.concact ("Def", "Ghi");
alert (str2);//outputs "Abcdefghi"

Properties and methods of the Javascript string class

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.