The purpose is very simple. I will mainly review some of the common attributes and methods of String in javascript. Although they are often used, some details are not noticed. It will be a waste of time to check them when they are used, therefore, I summarized my impressions
I. Several declaration methods of strings
Var str = 'Hello world! ';
Var str = String ('Hello world! ');
Var str = new String ('Hello world! ');
Var str = new Object ('Hello world! ');
Ii. common String attributes and Methods
StringObj. length
Purpose: return the total number of characters in a string.
'Abc'. length; // output: 3
StringObj. indexOf (searValue [, fromIndex])
Purpose: return the position where the searchValue first appears in stringObj. By default, the searchValue starts from position 0 unless it refers to the position fromIndex where the search starts.
Note: fromIndex is optional. If no searchValue is found,-1 is returned.
'Abc'. indexOf ('C'); // output 2
'Abc'. indexOf ('D'); // output-1
'Abc abc'. indexOf ('C', 3); // output 6
StringObj. lastIndexOf (searchValue [, fromIndex)
Purpose: return the last issue of searchValue in stringObj. By default, searchValue starts from the last character. If fromIndex is specified, search from the position of fromIndex from the back to the back.
Note: fromIndex is optional. If no searchValue is found,-1 is returned.
'Abc abc'. lastIndexOf ('C'); // output 6
'Abc abc'. lastIndexOf ('D'); // output-1
'Abc abc'. lastIndexOf ('C', 5); // output 2
StringObj. charAt (index)
Purpose: return the character at the specified position (index) of stringObj. The value of index is 0 <index <length
NOTE: If index> stringObj. length or index <0, an empty string "" is returned ""
'Abc'. charAt (1); // output B
'Abc'. charAt (10); // output ""
StringObj. toUpperCase ()
Purpose: Convert all lowercase letters in stringObj into uppercase letters.
'Abc'. toUpperCase (); // output abC
StringObj. toLowerCase ()
Purpose: Convert all uppercase letters in stringObj to lowercase letters.
'Abc'. toLowerCase (); // output ABc
StringObj. concat (string1, string2,... stringN)
Purpose: connect two or more strings. First, convert the stringX passed in to the string, and then connect to the end of stringObj in sequence.
Note: The concat method does not change stringObj itself.
Var str = 'abc ';
Var tmp = str. concat ('def '); abcdef
Console. log (tmp );
Console. log (str); // output abc
StringObj. substring (start [, end])
Purpose: intercept all the characters starting from start to end, including the characters at start, but not at end.
Note: end is an optional parameter. If this parameter is not specified, all characters starting from start to the end of stringObj are truncated. start and end are non-negative integers.
'Abcdef'. substring (1, 3); // output bc
'Abcdef '. substring (1); // output bcdef
'Abcdef'. substring (1, 1); // output ""
StringObj. slice (start [, end])
Purpose: intercept all the characters starting from start and before end of stringObj, that is, the characters at start, but not the characters at end. Both start and end can be negative values. If it is negative, it indicates the nth character counted from the last character. For example,-1 indicates the last character, and-2 indicates the second to last.
Note: end is an optional parameter. If this parameter is not specified, all characters starting from start to the end of stringObj are truncated.
'Abcdef'. slice (1, 3); // output bc
'Abcdef '. slice (1); // output bcdef
'Abcdef'. slice (-3); // output def
'Abcdef'. slice (-3, 5); // output de
StringObj. split (separator [, howator])
Purpose: Separate the separtor and split stringObj into a string array. Separator can be a string or regular expression. If it is a string, the separator is separator. If weak separator is a regular expression, the separator is a string that meets the specified pattern of separator.
Note: haow is an optional parameter, which indicates the maximum length of the string array obtained by splitting. If the length exceeds the length specified by howmany, the result is truncated. If howmany is a regular expression, and contains a sub-expression, the obtained array contains the string matched by the sub-expression
'Hello world'. split (''); // output ['hello', 'World']
'Abcdef'. split (''); // output ['A', 'B', 'C', 'D', 'E', 'F']
'AA bb cc dd ee'. split ('', 3); // output ['A'
'Aaa bbb ccc dd'. split (/\ s +/); // output ['aaa', 'bbb ', 'dd']
'Aaa _ B _ccc '. split (' _ B _ '); // output ['aaa', 'ccc']
'Aaa _ B _ccc '. split (' _ (B) _ '); // output ["aaa", "B", "ccc"]
StringObj. match (regexp)
Purpose: query the matching results of a specified regular expression in a string. The result is related to whether regexp has a specified global flag.
1) No g: only perform a search. If no matching string exists, null is returned. If a matching string exists, an array is returned, the 1st elements in the array store the results of regexp matching. The remaining elements in the array store the matching results of regexp subexpressions respectively. In addition, the returned results have two attributes, the input and index attributes are respectively stored in the original string stringObj, and the index is the position of the matched start character in stringObj.
2) g: perform a global search and return all matched substrings (excluding the matching results of the subexpression). If no matched strings exist, null is returned. If a matched string exists, all matched substrings in the returned array. Note that the index and input attributes are not available at this time.
// G + NO subexpression without global flag
Var result = 'abc123'. match (/12/); // output ['12']
Console. log (result. index); // output 3
Console. log (result. input); // output 'abc123'
// No global flag + no matching result
Result = 'abc123'. match (/def/); // output null
// G + subexpression without global flag
Result = 'abc123'. match (/bc (123)/); // output ['bc123', '123']
// Has a global flag + NO subexpression
Result = 'abc123abc123 '. match (/AB/g); // output [' AB ',' AB ']
// No global flag + subexpression
Result = 'abc123abc123 '. match (/abc (123)/); // output ['abc123', 'abc123']
StringObj. search (regexp)
Purpose: retrieve the substring that matches the regular expression regexp. If no matching result exists,-1 is returned. If a matching result exists, the start position of the first matching result is returned.
'Abcdef'. search (/\ d/); // output-1
'1970-456-789 '. search (/-\ d/); // output 3
StringObj. match (regexp, replacement)
Purpose: replace some strings in stringObj with another string. If regexp is a string, replace stringObj with the first substring that is equal to regexp. If regexp is a regular expression, check whether the global flag exists. If there is no g, replace the first substring; if there is g, replace all substrings;
Note: Pay attention to more, here only a few simple examples, for more reference, see http://www.w3school.com.cn/js/jsref_replace.asp
'Hello world'. replace ('World', 'casper '); // return 'Hello Casper'
'123abc123'. replace (/\ d +/, 'D'); // return 'abc123'
'123abc123'. replace (/\ d +/g, 'D'); // return 'dabcd'
'Abc123'. replace (/bc (12)/, function () {// output 'abc123'
Console. log (arguments [0]); // output 'bc12' (matched substring)
Console. log (arguments [1]); // output '12' (matching the first subexpression)
Console. log (arguments [2]); // output 1 (First Matching start position)
Console. log (arguments [3]); // output 'abc123' (original string)
Return arguments [0]. toUpperCase ();
});
Iii. Post
Some less commonly used methods, such as stringObj. charCodeAt, Or the implementation method is not explicitly stated in the standard, such as stringObj. substr, which is not described here. For more information, click the following link.