How to create String object
Declaration: The method of the String object can also be accessed in all basic string values.
Call the constructor String():
var str = new String(); var str = new String('hello world');//Initialize str, = 11;
How to access and search String
1. Access (via index)
(1) charAt() or []
1 parameter, the parameter is the character position, return the character
var strValue = new String('hello world'); ((1));//e (strValue[1]);//E,IE7 and below use this method, undefined will be returned
(2)charCodeAt()
1 parameter, the parameter is the character position, return the character encoding
var strValue = new String('hello world'); ((1));//101
2. Find the location
(1)indexOf()
The first parameter is the specified substring, and the second parameter is the search position. Returns the index, if not found, return -1
var str = 'hello world' ('l');//2, return the position of the first character found('l',6);//9
(2)lastIndexOf()
The difference from indexOf() is that lastIndexOf() is to search for substrings from the end of the string.
Character Method
1. Extended string
concat()
Accepts any number of parameters to splice one or more strings, return the splicing to get a new copy of the string.
var str = new String('hello'); var result = (' world'); (result);//hello world typeof result//"string"
2. Get substring method
slice(), substr(), substring(), these three methods will return a copy of the substring of the string being operated, and they will also accept 1 or 2 parameters, close first and then open [)
(1)slice()
var str = 'hello'; (0,2);//"he", the first parameter specifies the starting position of the string, and the second parameter indicates where the string ends(-3);//"llo",o represents -1, countdown in turn, -3 represents the third last l(-2,-1);//"l", Similarly, -2 represents the second to last l, -1 represents the last o
(2)substring()
var str = 'hello'; (0,2);//"he", the parameters at this time have the same meaning (0,2)(-3);//"hello", the substring() method will convert all negative values into 0(-3,-2);//"", same as above
(3)substr()
var str = 'hello'; (1,2);//"el", the first parameter specifies the starting position of the string, and the second parameter specifies the number of characters returned(-3);//"llo", the parameters at this time have the same meaning (-3)(-3,-1);//"", the substr() method will convert the negative second parameter to 0
There is a problem in IE when the substr() method passes negative values, it returns the original string, IE9 fixed this problem
3. Convert string to array
split()
Split the string into multiple substrings based on the specified delimiter (can be a string or a RegExp object), and put the result in an array, accepting an optional second parameter to specify the size of the array and returning the array.
var color = 'blue,red,orange'; ();//["red,blue,orange"], length is 1(',');//["blue", "red", "orange"], length is 3var color = 'blue-red-orange'; ('-');//["blue", "red", "orange"], length is 3(',',2);//["blue", "red"]
4. String case conversion
toLowerCase(),toUpperCase()
var str = 'hello'; ();//"HELLO" ();//"hello"
5. Method for deleting string spaces
trim()
Delete all spaces in the prefix and suffix in the string, and return a copy of the result.
var str = ' hello world '; ()//"hello world"
6. Pattern matching method of strings
(1)match()
Parameters: Only one parameter is accepted, either a regular expression or a RegExp() object.
Return: array. The first item in the array is a string that matches the entire pattern, and each item after that (if any) holds the string that matches the regular expression capture group.
Essentially the same as calling exec().
var text = 'cat, bat, sat, fat'; var pattern = /.at/; var matches = (pattern); matches // ["cat"] // "cat, bat, sat, fat" // 0
(2)search()
Parameters: Same as match() method.
Returns: Index of the first match in the string, and -1 if there is no match.
The search() method is always searched from front to back
var text = 'cat, bat, sat, fat'; var pattern = /at/; (pattern) // 1
(3)replace()
Parameter: Receive two parameters. The first parameter can be a RegExp object or a string (this string will not be converted into a regular expression), and the second parameter can be a string or a function.
If the first argument is a string, then only the first substring will be replaced. The only way to replace all substrings is to provide a regular expression and specify the global flag (g) flag.
If the second parameter is a string, you can also use some special character sequences to insert the values obtained by the regular expression operation into the result string.
It can also be a function, and the parameters passed to the function are the matches of the pattern, the position of the matches of the pattern in the string, and the original string. In the case where the regular expression defines multiple capture groups, the parameters passed to the function are the matches of the pattern, the matches of the first capture group, and so on, but the last two parameters are the position of the matches of the pattern in the string and the original string.
Character sequence | Replace text |
---|---|
$$ | $ |
$& | Match substrings in the entire pattern |
$' | Substring after matching substring |
$` | Substring before matching string |
$n | Match the substring in the nth capture group, $1 |
$nn | Match the substring of the nn-th capture group, $01 |
var text = 'xxx-love-xxx'; var pattern = /xxx/g; var result = (pattern,'2') result// "2-love-2" (/(xxx)-\w{4}-(xxx)/g,'I love YOU');//"I love YOU" var text = 'xxx-love-xxx'; var pattern1 = /xxx/g; var result = (pattern1,'$$') result// "$-love-$" var result = (pattern1,'$&2') result//"xxx2-love-xxx2" var result = (pattern1,'$\'') result//"-love-xxx-love-"