Home > Article > Web Front-end > Summary of commonly used string methods in js and es6 (collection)
Commonly used strings in js and es6, for example: slice(start,end) -> Intercept string, usage: The usage of slice is basically the same as the usage of substring, but the difference is: 1.slice(start,end ) -> start cannot be greater than end, otherwise an empty string will be returned;
2.slice can accept a negative number as a parameter. If it is a negative number, the rules will be as follows: add the length of the string and the assignment, and replace this Value
Usage:
1.substring(start,end) -> Use To express a range in a mathematical expression, intercept [start, end);
2.substring(start,end),end > start -> The same result as above will be automatically switched, but both start and end must be is a positive number. If both start and end are empty, return the original string (meaningless)
3.substring(start) -> Without end, it is equivalent to [start, the last character]
let str = 'Hello world'; let use1 = str.substring(0, 3); console.log(use1); // Hel let use2 = str.substring(3,0); console.log(use2); // hel let use3 = str.substring(2); console.log(use3); // llo world
Usage:
The usage of slice is basically the same as that of substring, the only difference is:
1.slice( start, end) -> start cannot be greater than end, otherwise an empty string will be returned;
2.slice can accept a negative number as a parameter. If it is a negative number, the rules will be as follows: add the length of the string and the assigned value, Replace this value. For example:
let str = 'abcdefg' // length = 7 str.slice(1,-4) // bc -> str.slice(1,7-4) -> str.slice(1,3)
Usage:
1.substr(start,length) -> The intercepted string interval is: [start,start length)->Start from start, count the number of start and length strings;
2 .substr(start) -> The intercepted string interval is: [start, last character]
let str = 'Hello world'; console.log(str.substr(1,2)) // el console.log(str.substr(3)) // lo world
1.char: is the character you are looking for, index: is the position number of the character to start looking for (if not, it is the leftmost character in indexOf, in lastIndexOf is the rightmost character);
2. indexOf searches from left to right, while lastIndexOf searches from right to left;
3. Their return values are all the position numbers where the char is found, If not found, return -1
let str = 'good'; console.log(str.indexOf('o')); // 1 console.log(str.lastIndexOf('o')); // 2
charAt(index) returns the character at index position , charCodeAt(index) returns the character Unicode code at index position
charAt(index) cannot recognize characters greater than 0xFFFF. At this time, you can use at() to identify
var str = 'abc' str.charAt(0) // a str.charCodeAt(0) // 97
Related articles:
Detailed explanation of string templates in ES6
Related Video:
Javascript - ES6 Practical Video Course - Free Online Video Tutorial
The above is the detailed content of Summary of commonly used string methods in js and es6 (collection). For more information, please follow other related articles on the PHP Chinese website!