JS reference type (6)--Basic Package Type 2

Source: Internet
Author: User

(3) String type

The string type is a wrapper type for strings and can be created using the string constructor.

var new string ("Hello World");

The method of a string object can also be accessed in all basic string values. Where the inherited valueof (), tolocalstring (), ToString () methods return the base string value represented by the object.
Each instance of the string type has a length property that indicates that the string contains more than one character.

1 "character method
    • CharAt () Method: accepts a parameter, based on the 0 character position. Returns the character of the given position as a single string.
      var stringvalue = "Hello World"; alert (Stringvalue.charat (//"E"
    • charCodeAt () Method: accepts a parameter, based on the 0 character position. Returns the character encoding for the given position.
      var stringvalue = "Hello World"; alert (Stringvalue.charat (//101
    • ECMAScript methods for accessing individual characters. In browsers that support this method (ie8+ and others), you can use square brackets and numeric indexes to access specific characters in a string. This syntax is used in IE7 and earlier versions, and the undefined value is returned.
      var stringvalue = "Hello World"; alert (stringvalue[//"E"
2 "String manipulation methods
    • Concat () method: Used to stitch one or more strings together to return a new string of stitching. You can accept any number of parameters. "Use + operator"
      var stringvalue = "Hello"; var result = Stringvalue.concat ("World", "!"  //"Hello world!" // "Hello"
  • Slice (), substr (), and substring (). All three methods return a substring of the manipulated string, and also accept one or two parameters. The first parameter specifies the starting position of the substring, and the second parameter indicates where the substring ends. Specifically, the second parameter of Slice () and substring () specifies the position after the last character of the substring. The second parameter of substr () specifies the number of characters returned. If these methods are not passed a second argument, the length of the string is used as the end position. As with the concat () method, Slice (), substr (), and substring () do not modify the value of the string itself, simply returning a string value of the base type.
    var stringvalue = "Hello World",alert (Stringvalue.slice (//"Lo World"// "Lo World" // "Lo World" // "Lo W" // "Lo W" // "Lo worl"

In cases where the arguments passed to these methods are negative. The slice () method adds the passed negative value to the length of the string, and the substr () method adds the negative first argument to the length of the string, and the negative second argument to 0. Finally, the substring () method converts all negative arguments to 0.

var stringvalue = "Hello World"; alert (Stringvalue.slice (//"Rld"//  "Hello World"//"Lrld"//"Lo w"//"hel"// " "

The JavaScript implementation of IE has a problem in handling cases where negative values are passed to substr (), and it returns the original string. IE9 fixed the problem.

3 "string Position method
    • IndexOf () Method: Searches backward from the beginning of a string for the given substring, then returns the position of the substring, or 1 if not found.
    • LastIndexOf () Method: Searches for the given substring forward from the end of a string, then returns the position of the substring, or 1 if not found.
var stringvalue = "Hello World"; alert (Stringvalue.indexof (//4//7 

Both methods can receive the optional second argument, which indicates where to start the search from within the string.

var stringvalue = "Hello World"; alert (Stringvalue.indexof (//7// 4

With the second parameter, you can call the indexof () or LastIndexOf () method by looping to find all the matched substrings.

var stringvalue = "Lorem ipsum dolor sit amet,consectetur adipisicing elit"; var New Array (); var pos = stringvalue.indexof ("e");  while (pos>-1= Stringvalue.indexof ("E", pos+1//"3,24,32,35,52"
4 "trim () method

The trim () method creates a copy of the string, removes any spaces from the predecessor and suffix, and then returns the result.

var stringvalue = "Hello World"; var trimmedstringvalue =//"Hello World"//"Hello World"

Browsers that support this method have ie9+ and others. In addition to ie9+, other browsers support non-standard trimleft () and TrimRight () methods, respectively, to remove spaces at the beginning and end of a string.

5 "String Casing conversion method

toLowerCase (), Tolocallowercase (), toUpperCase (), Tolocaluppercase ()

6 "pattern matching method for strings
    • Match () Method: This method is called on a string, essentially the same as the Exec () method that calls RegExp. The match () method accepts only one parameter, either a regular expression or a RegExp object. Returns an array.
       var  text = "Cat,bat,sat,fat"  var  pattern =/.at/;  //  same as pattern.exec (text)  var  matches = Text.match (pattern); alert ( Matches.index);  // 0  alert (matches[0]); //  alert (Matches.lastindex); // 0  
    • Search () Method: This method has the same argument as the match () method, a regular expression specified by a string or RegExp object. Returns the index of the first occurrence in a string. If no match is found, 1 is returned. This method is always the backward lookup mode from the beginning of the string.
       var  text = "Cat,bat,sat,fat"  var  pos = text.search (/at/ // 1  
    • Replace () method: Replace substring. This method accepts two parameters, the first parameter can be a RegExp object or a string (the string will not be converted to a regular expression), the second argument can be a string or a function. If the first argument is a string, only the first substring is replaced. The only way to replace all substrings is to provide a regular expression, and to specify the Global (g) flag.
      var text = "Cat,bat,sat,fat"; var result = Text.replace ("At", "ond"//"Cond,bat,sat,fat"= Text.replace (/at/g, "ond"//"Cond,bond,sond,fond"

If the second argument is a string, you can use some special sequence of characters to insert the value of the regular expression operation into the result string.
The second parameter of the replace () method can also be a function. In the case of only one match, 3 parameters are passed to the function: the match of the pattern, the position of the pattern match in the string, and the original string.

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;
    • Split () Method: Splits a string into multiple substrings based on the specified delimiter and places the result in an array. This method takes the optional second parameter, which specifies the size of the array, to ensure that the returned array does not exceed the specified size.
      var colortext = "Red,blue,green,yellow"; var // ["Red", "Blue", "green", "yellow"] var // ["Red", "blue"] var // ["", ",", ",", ",", ""]
7 "Localecompare () method

The Localecompare () method compares two strings and returns one of the following values:

    1. If the string should precede the string parameter in the alphabet, a negative number is returned (in most cases-1, depending on the implementation);
    2. Returns 0 if the string equals the string argument;
    3. If the string should be followed by a string parameter in the alphabet, a positive number is returned (1 in most cases, depending on the implementation).
8 "fromCharCode () method

Accept one or more character encodings, and then convert them to a string.

// "Hello"

JS reference type (6)--Basic Package Type 2

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.