A summary of JavaScript learning every day (String object) _javascript Tips

Source: Internet
Author: User
Tags numeric value

1, functions that can be passed in a string object are described in

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, not the position of the string. Syntax Stringobject.match (searchvalue) stringobject.match (regexp) searchvalue required.
  Specify the string value to retrieve. RegExp required. The RegExp object that prescribes the pattern to match.
 If the parameter is not a RegExp object, you need to pass it to the RegExp constructor first and convert it to a RegExp object. 
 * * var text = "Cat, bat, Sat, fat";
 
 var pattern =/.at/; 
 var matches = Text.match (pattern); alert (Matches.index);  0 alert (matches[0]); "Cat" alert (pattern.lastindex);
  0/* Definition and usage the search () method retrieves the substring specified in the string, or retrieves a substring that matches the regular expression.
  Stringobject.search (regexp) regexp This parameter can be a substring that needs to be retrieved in Stringobject, or it can be a RegExp object that needs to be retrieved.
  Note: To perform a retrieval that ignores case, append flag I.
  Returns the starting position of the first substring in the value stringobject that matches the regexp.
 Note: If no matching substring is found, return-1.
 * * var pos = text.search (/at/); Alert (POS);
  1/* Definitions and usages the replace () method replaces some characters in a string with some other characters, or replaces a substring that matches a regular expression. Stringobject.replace (regexp/substr,replacement) regexp/substr required.
  The RegExp object that prescribes the substring or pattern to be replaced.
 Note that if the value is a string, it is used as the direct text pattern to retrieve, not first converted to the RegExp object. Replacement required. A string value.
  Provides a function that replaces text or generates alternate text.
 Returns the value of a new string, which is obtained after the first match of RegExp or all matches are replaced with replacement.
 * * var result = 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) function Htmlescape (text) {return text.replace (/[<>) &]/g,
   function (match, POS, originaltext) {switch (match) {case ' < ': return ' < ';
   Case ">": Return ">";
   Case "&": Return "&";
  Case "" "": Return "";
 }  
  }); Alert (Htmlescape ("<p class=\" greeting\ ">hello world!</p>"));
  <p class= "Greeting" >hello world!</p>/* 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 the Stringobject from the place specified by the parameter. Howmany Optional. This parameter specifies the maximum length of the array to return. If this argument 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 itsLength. 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 a string that matches the subexpression (but does not include text that matches the entire regular expression).
 * * var colortext = "Red,blue,green,yellow"; var colors1 = Colortext.split (","); ["Red", "Blue", "green", "yellow"] var colors2 = Colortext.split (",", 2); ["Red", "Blue"] var Colors3 = Colortext.split (/[^\,]+/);
 //["", ",", ",", ",", ""]

2, the string into 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 ());

3, String Object new String ()

 var stringobject = new String ("Hello World");
 var stringvalue = "Hello World";
 
 Alert (typeof Stringobject); "Object"
 alert (typeof stringvalue);//"String"
 alert (stringobject instanceof string);//true
 Alert (StringValue instanceof String); False

4. String fromCharCode () method


  /* Definitions and usages
  fromCharCode () can accept a specified Unicode value, and then return a string.
  String.fromCharCode (numx,numx,..., numx)
  numx required. One or more Unicode values, that is, the Unicode encoding of the characters in the string to be created.
 */
 alert (String.fromCharCode, 108, 108);//"Hello"

5. String local comparison method Localecompare ()

The
/* Definition and usage compares two strings in a local-specific order.
  Stringobject.localecompare (target) target a string to compare with Stringobject in a local-specific order. The return value describes the number of the comparison result.
  If Stringobject is less than target, Localecompare () returns a number less than 0. If the 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. 
 * * var stringvalue = "Yellow"; Alert (Stringvalue.localecompare ("Brick")); 1 Alert (Stringvalue.localecompare ("yellow")); 0 alert (Stringvalue.localecompare ("Zoo")); -1//preferred technique for using Localecompare () function Determineorder (value) {var = stringvalue.local
  Ecompare (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 () method


  /* Definitions and usages the
  indexOf () method returns the position of the first occurrence of a specified string value in a string.
  Stringobject.indexof (searchvalue,fromindex)
  searchvalue required. A string value that requires retrieval.
  fromindex an optional integer parameter. Specify 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.
  
  definitions and usages the
  LastIndexOf () method returns the last occurrence of a specified string value, which is searched forward at the specified position in a string.
  Stringobject.lastindexof (searchvalue,fromindex)
  searchvalue required. A string value that requires retrieval.
  fromindex an optional integer parameter. Specify where to start retrieving in the string.
  its legal value is 0 to stringobject.length-1. If this argument is omitted, the retrieval begins at the last character of the string.
 * *
 var stringvalue = "Hello World";
 Alert (Stringvalue.indexof ("O"));  4
 Alert (Stringvalue.lastindexof ("O"));//7
 alert (stringvalue.indexof ("O", 6));  7
 Alert (Stringvalue.lastindexof ("O", 6));//4 

7, the string commonly used character interception method

/* Definitions and usages the slice () method returns 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, then it sets the position from the end of the array.
  In other words,-1 refers to the last element, 2 to the penultimate element, and so on. End is optional. Specify where to end the selection. The parameter is the array subscript at the end of the array fragment.
  If this argument is not specified, the Shard array contains all the elements from start to the end of the array.
  If this argument is a negative number, it sets the element that starts at the end of the array.
  The return value returns a new array containing the elements in the arrayobject from start to end (excluding the element). Note that the method does not modify the array, but instead returns a child array.
  
  
  If you want to delete a section of an element in an array, you should use Method Array.splice ().
  Definitions and usages the substring () method extracts characters from a string mediation between two specified subscripts. Syntax stringobject.substring (start,stop) start required.
  A non-negative integer that stipulates the position of the first character of the substring to be extracted in the stringobject. Stop is optional. A non-negative integer that is 1 more than the last character of the substring to extract in the Stringobject.
  If this argument is omitted, the returned substring continues to the end of the string.
  Returns a new string that contains a substring of stringobject with all characters from the start to the stop-1, whose length is stop minus start.
  Description The substring returned by the substring () method includes the character at the start, but does not include the character at the stop.
  If the argument start is equal to the stop, the method returns an empty string (that is, a length of 0).
  
  
  If start is larger than stop, the method swaps the two parameters before extracting the substring.
  Definitions and usages the substr () method extracts a specified number of characters from the start subscript in a string. Syntax Stringobject.substr (start,length) start required. The starting subscript of the substring to extract. Must be a numeric value. If it is a negative number, then theThe position at which the parameter declaration starts at the end of the string.
  In other words,-1 refers to the last character in the string, 2 to the penultimate character, and so on. Length is optional. The number of characters in the substring. Must be a numeric value.
  If this argument is omitted, the string from the start position of the stringobject to the end is returned.
  Returns a new string that contains the length characters starting at the start of the Stringobject, including the character to start.
  
  
 If length is not specified, the returned string contains the character ending from start to Stringobject.
 * * var stringvalue = "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)

Above is today's JavaScript learning Summary, and will continue to update every day, I hope you continue to pay attention.

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.