JavaScript Learning Summary (vii) String object

Source: Internet
Author: User

1, the function of the string object can be passed the regular description

/*The Match () method retrieves the specified value within a string, or finds a match for one or more regular expressions.            The method is similar to IndexOf () and lastIndexOf (), but it returns the specified value instead of the position of the string. Syntax Stringobject.match (searchvalue) stringobject.match (regexp) searchvalue required.            Specifies the string value to retrieve. RegExp required. A RegExp object that specifies the pattern to match.        If the parameter is not a RegExp object, you need to first pass it to the RegExp constructor and convert it to a RegExp object. */        varText = "Cat, bat, Sat, fat"; varPattern =/.at/; varMatches =Text.match (pattern);        alert (Matches.index); //0Alert (matches[0]);//"Cat"alert (Pattern.lastindex);//0        /*define and use the search () method to retrieve a substring specified in a string, or to retrieve a substring that matches a regular expression.            Stringobject.search (regexp) regexp This parameter can be a substring that needs to be retrieved in Stringobject, or a RegExp object that needs to be retrieved.            Note: To perform a case-insensitive retrieval, append the flag I.            Returns the starting position of the first substring in the value stringobject that matches the regexp.        Note: If no matching substrings are found, 1 is returned. */        varpos = Text.search (/at/);   Alert (POS); //1                /*definitions and usages the replace () method is used to replace some characters with some character in a string, or to replace a substring that matches a regular expression. Stringobject.replace (regexp/substr,replacement) regexp/substr required.            A RegExp object that specifies the substring or pattern to replace.            Note that if the value is a string, it is used as the direct volume text pattern to be retrieved, instead of being converted to the RegExp object first. Replacement required. A string value.            A function that specifies replacement text or generates alternate text.        The return value of a new string is replaced with replacement after the first match of RegExp or all of the matches are obtained. */        varresult = Text.replace ("At", "ond");    alert (result); //"cond, Bat, Sat, fat"result= Text.replace (/at/g, "ond");    alert (result); //"Cond, Bond, Sond, fond"result= Text.replace (/(. at)/g, "Word ($)");    alert (result); //Word (cat), Word ( bat), Word (Sat), Word (FAT)                functionHtmlescape (text) {returnText.replace (/[<> "&]/g,function(Match, POS, originaltext) {Switch(match) { Case"<":                        return"&lt;";  Case">":                        return"&gt;";  Case"&":                        return"&amp;";  Case"\"":                        return"&quot;";        }                         }); } alert (Htmlescape ("<p class=\" greeting\ ">hello world!</p>");//&lt;p class=&quot;greeting&quot;&gt; Hello world!&lt;/p&gt;        /*definition and usage the split () method is used to split a string into an array of strings. Stringobject.split (Separator,howmany) separator required.            A string or regular expression that splits stringobject from where specified by the parameter. Howmany is optional. This parameter specifies the maximum length of the returned array. If this parameter is set, the returned substring will not be more than the array specified by this parameter.            If this argument is not set, the entire string is split, regardless of its length. Returns a string array of values. The array is created by splitting the string stringobject into substrings at the boundary specified by separator.            The string in the returned array does not include the separator itself.        However, if separator is a regular expression that contains a subexpression, the returned array includes the strings that match those sub-expressions (but not the text that matches the entire regular expression). */        varColortext = "Red,blue,green,yellow"; varColors1 = Colortext.split (",");//["Red", "Blue", "green", "yellow"]        varColors2 = Colortext.split (",", 2);//["Red", "blue"]        varColors3 = Colortext.split (/[^\,]+/);//["", ",", ",", ",", ""]

2. The string is converted to lowercase and uppercase functions

  var stringvalue = "Hello World";        Alert (Stringvalue.tolocaleuppercase ());   // "HELLO World"        Alert (Stringvalue.touppercase ());        // "HELLO World"        Alert (Stringvalue.tolocalelowercase ());  // "Hello World"        Alert (Stringvalue.tolowercase ());        // "Hello World"

3. String Object new String ()

  var New String ("Hello World");         var stringvalue = "Hello World";                Alert (typeof Stringobject);   // "Object"        Alert (typeof StringValue);    // "string"        instanceof String);  // true        instanceof String);   // false

4. String fromCharCode () method

/*             definition and Usage            fromcharcode () can accept a specified Unicode value and then return a string.            String.fromCharCode (numx,numx,..., numx)            numx    required. One or more Unicode values, the Unicode encoding of the characters in the string to be created.         */         alert (string.fromcharcode (//"Hello"

5. String local comparison method Localecompare ()

/*definitions and usages compare two strings in a local-specific order.            Stringobject.localecompare (target) The string to compare to Stringobject in a local-specific order. The return value indicates the number of the comparison result.            If Stringobject is less than target, Localecompare () returns a number less than 0. If Stringobject is greater than target, the method returns a number greater than 0.        If two strings are equal, or if there is no difference based on the local collation, the method returns 0. */        varStringValue = "Yellow"; Alert (Stringvalue.localecompare ("Brick"));//1Alert (Stringvalue.localecompare ("yellow"));//0Alert (Stringvalue.localecompare ("Zoo"));//-1                //preferred technique for using localecompare ()        functionDetermineorder (value) {varresult =Stringvalue.localecompare (value); if(Result < 0) {alert ("The string ' yellow ' comes before the string '" + Value + "'."); } Else if(Result > 0) {alert ("The string ' yellow ' comes after the string '" + Value + "'."); } Else{alert ("The string ' yellow ' is equal to the string ' + Value +" '. "); }} determineorder ("Brick"); Determineorder ("Yellow"); Determineorder ("Zoo");

6, IndexOf () and LastIndexOf () methods

/*The define and use IndexOf () method returns the position of the first occurrence of a specified string value in a string. Stringobject.indexof (Searchvalue,fromindex) searchvalue required.            Specifies the string value that needs to be retrieved. Fromindex an optional integer parameter.            Specifies where to start retrieving in the string. Its legal value is 0 to stringobject.length-1.                        If this argument is omitted, it is retrieved from the first character of the string.            Define and use the LastIndexOf () method to return a specified string value to the last occurrence of the position, in a string at the specified position from the back forward search. Stringobject.lastindexof (Searchvalue,fromindex) searchvalue required.            Specifies the string value that needs to be retrieved. Fromindex an optional integer parameter.            Specifies where to start retrieving in the string. Its legal value is 0 to stringobject.length-1.        If this argument is omitted, the search begins at the last character of the string. */        varstringvalue = "Hello World"; Alert (Stringvalue.indexof ("O"));//4Alert (Stringvalue.lastindexof ("O"));//7Alert (Stringvalue.indexof ("O", 6));//7Alert (Stringvalue.lastindexof ("O", 6));//4

7, the string characters commonly used character interception method

/*            Define and use the slice () method to return the selected element from an existing array. Arrayobject.slice (start,end) start required. Specify where to start the selection. If it is a negative number, it specifies the position from the end of the array.            In other words, 1 refers to the last element, 2 refers to the second-lowest element, and so on. End is optional. Specifies where to end the selection. The parameter is the array subscript at the end of the array fragment.            If this parameter is not specified, then the segmented array contains all elements from start to end of the array.            If this parameter is a negative number, it specifies the element starting from the end of the array.            The return value returns a new array containing the elements from start to end (excluding the element) in the Arrayobject. Note that the method does not modify the array, but rather returns a sub-array.                                    If you want to delete an element from an array, you should use Method Array.splice ().            The definition and usage of the substring () method is used to extract the character of a string intermediary between two specified subscripts. Syntax stringobject.substring (start,stop) start required.            A nonnegative integer that specifies the position of the first character of the substring to be extracted in the stringobject. Stop is optional. A nonnegative integer that is 1 more than the last character of the substring to extract in Stringobject.            If this argument is omitted, the returned substring continues until the end of the string.            The return value is a new string that contains a substring of stringobject whose contents are all characters from start to stop-1, with a length of stop minus start.            Description The substring returned by the substring () method includes the character at start, but does not include the character at the stop. If the parameter startStop is equal, the method returns an empty string (that is, a string of length 0).                                    If start is larger than stop, the method swaps both parameters before extracting the substring.            Define and use the substr () method to extract a specified number of characters from the start subscript in a string. Syntax Stringobject.substr (start,length) start required. The starting subscript for the substring to extract. Must be numeric.                    If it is a negative number, the argument declares the position from the end of the string.            That is,-1 refers to the last character in the string, 2 refers to the second-lowest character, and so on. Length is optional. The number of characters in the substring. Must be numeric.            If this argument is omitted, then the string from the beginning of the stringobject to the end is returned.            The return value is a new string that contains the length character starting at the start of the Stringobject, including the character that the start refers to.                                 If length is not specified, the returned string contains the character from start to the end of Stringobject.*/        varstringvalue = "Hello World"; Alert (Stringvalue.slice (3));//"Lo World"Alert (stringvalue.substring (3));//"Lo World"Alert (STRINGVALUE.SUBSTR (3));//"Lo World"Alert (Stringvalue.slice (3, 7));//"Lo W"Alert (stringvalue.substring (3,7));//"Lo W"Alert (STRINGVALUE.SUBSTR (3, 7));//"Lo worl"alert (Stringvalue.slice (-3));//"Rld"Alert (Stringvalue.substring (-3));//"Hello World"Alert (STRINGVALUE.SUBSTR (-3));//"Rld"Alert (Stringvalue.slice (3,-4));//"Lo W"Alert (stringvalue.substring (3,-4));//"Hel"Alert (STRINGVALUE.SUBSTR (3,-4));//"" (empty string)

JavaScript Learning Summary (vii) String object

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.