Home >Web Front-end >JS Tutorial >js intercepts string
In JavaScript, you can use the slice(), substring() and substr() methods of strings to intercept strings: 1. The slice() method accepts two parameters, the starting index and the ending index. , returns the substring from the start index to the end index but not including the end index; 2. The substring() method accepts two parameters, the start index and the end index, and returns the substring from the start index to the end index but not Including substrings between ending indices and so on.
#In JavaScript, you can use the slice(), substring() and substr() methods of strings to intercept strings. These methods have different usages and parameters, and you can choose the appropriate method according to your specific needs.
let str = 'Hello, World!'; let result = str.slice(7, 12); // 截取从索引7到索引12之前的字符 console.log(result); // 输出 "World"
let str = 'Hello, World!'; let result = str.substring(7, 12); // 截取从索引7到索引12之前的字符 console.log(result); // 输出 "World"
let str = 'Hello, World!'; let result = str.substr(7, 5); // 从索引7开始截取5个字符 console.log(result); // 输出 "World"
No matter which method is used, you can choose the appropriate method to intercept the string according to the specific needs. It should be noted that these methods all return a new string and the original string will not be modified.
The above is the detailed content of js intercepts string. For more information, please follow other related articles on the PHP Chinese website!