(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
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
- 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"<"; Case">":return">"; Case"&":return"&"; Case"\"":return""";}});} Alert (Htmlescape ("<p class = \" greeting\ ">hello world!</p>"));//<p class="greeting"> Hello world!</p>
- 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:
- If the string should precede the string parameter in the alphabet, a negative number is returned (in most cases-1, depending on the implementation);
- Returns 0 if the string equals the string argument;
- 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