1, String segmentation
1,slice
Syntax: slice (start, end) (do not change the original string) Applies to strings and arrays.
(1) The element with subscript end is not included when intercepting the string.
(2) End is an optional parameter. If not, all strings from start to end will be used by default.
(3) When the parameter of slice method is negative, the length of string / array is added first
If the first parameter (table start position) or the second parameter (table end position) is negative, its corresponding index is this parameter Number plus the length of the string. If it is still negative at this time, the starting index is 0.
(4) Return empty string '' or empty array []
- When the negative parameters plus the string length become positive, if the first parameter (start) is greater than or equal to the second parameter (end), no character or array can be obtained, and the empty string "" / empty array [] is returned. (start>end)
- If the start index is greater than or equal to the length of the array, slice() returns an empty string '' or an empty array []. (start>length)
- If the end index is negative, the array length plus this value is the end index. If it is still negative, an empty string '' or an empty array [] is returned. (end<0)
var str = "abcdef"; console.log(str.slice(-3)) // 'def' // str.slice(-3) is equivalent to str.slice(5) with only one parameter. By default, all strings are from the character with index 5 to the end console.log(str.slice(-4, 7)); // "cdef" //The string length is 6, "abcdef".slice(-4,7) is equivalent to "abcdef".slice(2,7) //Note: there is a small point here: 7 is larger than the maximum index 5 of the string (starting from 0), //However, no error will be reported and undefined will not be displayed. The result is all strings from the character with index 2 to the end console.log(str.slice(-7, 3)); // ---->'abc' //Equivalent to "abcdef".slice(-1,3) plus length, the parameter is also negative, //The start index is 0, so execute "abcdef".slice(0,3) console.log(str.slice(-2, -4)); // ---->'' //Equivalent to "abcdef".slice(4,2) stat > end here, so '' console.log(str.slice(7)); //---> '' //The start index is greater than or equal to the length of the array, so '' is returned console.log(str.slice(1, -7)); //---> '' //End index plus length is still less than 0, so '' is returned
2,substr
Syntax: substr (start, length) Applies only to strings
Returns the number of characters substr (start, length) from the start position to the specified length (without changing the original string). Start is the index and length is the character length calculated from the start index (start is required and length is optional)
Returns null when the start index is equal to or greater than the string length
When the start index is negative, the character length needs to be added for calculation. If it is still negative at this time, it is regarded as 0
When length is 0 or negative, it returns null. If this parameter is omitted, it will be intercepted to the end
var str="0123456789"; console.log(str.substr(1,5))//12345 intercept 5 lengths from index 1 console.log(str.substr(10,5))//The start index is equal to the length of the string and returns null console.log(str.substr(12,5))//The start index is greater than the string length and returns null console.log(str.substr(-5,5))//56789 starts with 5 indexes and intercepts 5 later console.log(str.substr(-12,5))//01234 start with index 0 and intercept 5 later console.log(str.substr(-5,0))//Returns null when length is 0 console.log(str.substr(-12,-5))//When length is negative, null is returned console.log(str.substr(0))//0123456789 omit the second parameter and intercept to the end console.log(str.substr(-12))// 0123456789 start index 0 omit the second parameter and intercept to the end
3,substring
Syntax: substring (start, end) The substring method applies only to strings
Basically the same as slice method, it returns the part between the specified two subscript positions; Do not contain characters at end position (do not change the original string)
If any parameter is less than 0, it will be regarded as 0. If both parameters are negative or equal, null characters will be returned
When any parameter is greater than the character length, the index is the character length
When the start index is greater than the end index, the two indexes exchange positions
var str="0123456789"; console.log(str.substring(1,5)) //1234 start index is 1 and end index is 5 (excluding 5) //If any parameter is less than 0, it will be regarded as 0. If both parameters are negative or equal, null characters will be returned console.log(str.substring(-1,5)) //01234 start index is 0 and end index is 5 (excluding 5) console.log(str.substring(-1,-5)) //Return null console.log(str.substring(1,1)) //Return null //When any parameter is greater than the character length, the index is the character length console.log(str.substring(11,15)) //Return null. The start index is 10 and the end index is 10. Return null console.log(str.substring(1,15)) //123456789 the start index is 1 and the end index is 10 in total length (truncated to the last) //When the start index is greater than the end index, the two indexes exchange positions console.log(str.substring(15,1)) //The exchange position of the two parameters is the same as above, and 123456789 is returned
Before explaining the fourth method, let's review the differences and uses of the above three methods:
(1) : both receive two parameters. slice and substring receive the start position and end position (Note: the end position is not included), while substr receives the start position and the length of the string to be returned.
(2) : it should be noted that substring starts with the smaller one of the two parameters and ends with the larger one. (6, 3) will change to (3, 6) by default, and the small one will be processed as the starting position.
(3) : how to resolve when the parameter is negative.
slice: adds the length of the string to the corresponding negative number, and takes the result as a parameter.
substr: only the result of adding the first parameter to the string length will be taken as the first parameter (the first parameter is a negative number).
substring: directly convert the negative parameter to 0.
4,split
Syntax: split (separator, howmany)
This method divides a string into an array of strings.
1. Separators can be strings or regular expressions.
2. howmany optional parameter, indicating the maximum length of the returned array.
console.log('|a|b|c|'.split(/\|/)); // ['', 'a', 'b', 'c', ''] console.log('0123456789'.split('', 5)); // ['0', '1', '2', '3', '4']
2, Other string processing methods
1,charAt
.charAt(index) Returns a character at a specified position.
The index parameter is required. A number that represents a position in a string, that is, the subscript of a character in the string.
The subscript of the first character in the string is 0. If the parameter index is not between 0 and string.length, the method returns an empty string.
console.log('0123456789'.charAt(1)); // '1' returns characters with index 1 //Less than 0 or greater than or equal to the length of the string string string.length, which returns an empty string. console.log('0123456789'.charAt(-1)); // '' console.log('0123456789'.charAt(11)); // ''
2,concat
Used to connect two or more strings. Compared to Array.concat(). In fact, we don't use many strings. We use the plus sign (+) more
console.log('C'.concat('a', 't')); // 'Cat'
3,indexOf(searchString,position)
Find another string within string searchString. If it is found, the position of the first matching character is returned, otherwise - 1 is returned.
The optional parameter position can be set to start searching from a specified position of the string.
console.log('abcdabcd'.indexOf('c')); // 2 console.log('abcdabcd'.indexOf('e')) // -1 console.log('abcdabcd'.indexOf('c', 3)) //6 //Let it start looking at the position with index 3, so the position where c appears for the first time is 6.
4,lastIndexOf(searchString,position)
It is similar to the indexOf method, except that it starts at the end of the string, not at the beginning.
The search direction is reverse and the index is positive. The following code:
console.log('abcdabcd'.lastIndexOf('c')); // 6 console.log('abcdabcd'.lastIndexOf('c', 5)) //2
5,replace(searchValue,replaceValue)
Function: the replace method finds and replaces strings and returns a new string.
The parameter searchValue can be a string or a regular expression object.
The first case: if searchValue is a string, searchValue will only be replaced at the first occurrence.
console.log('abc_abc_abc'.replace('_', '-')); // abc-abc_abc
The second case: if searchValue is a regular expression with the g identifier, it will replace all matches. If it is not marked with g, it will only replace the first match.
The third case: replaceValue can be a string or a function (the above case)
6,search(regexp)
It is similar to the indexOf method.
It only accepts a regular expression object as an argument, not a string. If a match is found, it returns the first matching phonetic character position. If no match is found, it returns - 1. This method ignores the g ID and has no position parameter.
7. Case conversion
(1)toLowerCase()
(2)toUpperCase()
After conversion, all strings become lowercase (uppercase), and the original string will not be changed, which will not affect non alphabetic characters
console.log('hello world123'.toUpperCase());// HELLO WORLD123