Home > Web Front-end > JS Tutorial > body text

Summary of commonly used string methods in js and es6 (collection)

php是最好的语言
Release: 2018-07-27 14:47:47
Original
5161 people have browsed it

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

1.substring(start,end) -> Intercept string

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
Copy after login

2. slice(start,end) -> Intercept string

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)
Copy after login

3.substr(start,length) -> Intercept the string at the specified position and length

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
Copy after login

4.split()

5.indexOf(char,index ) and lastIndexOf(char,index)

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
Copy after login

6.charAt(index) and charCodeAt(index) and at(index) (es6 attribute)

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
Copy after login

Related articles:

Detailed explanation of string templates in ES6

Analysis of the representation methods of multi-line strings and connection strings in ES6 and related operation skills

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!