There are three commonly used string interception functions: substr substring slice. The calling method is as follows:
stringObject.slice(start,end)
stringObject.substr(start,length)
stringObject.substring(start,end)
The most obvious one is substr, no. The two parameters are length, which is the interception length. The second parameter of the other two functions is the subscript of the last character (the character of the subscript is not included here, only the character before the character is intercepted)
Compared with slice and substring, slice subscript can be a negative number, such as -1 represents the last character, but substring cannot. If substring start is larger than end, then these two parameters will be exchanged before extracting the substring, but slice will not. Slice will return an empty string
Example:
var str="Helloworld"
console.log(str.substr(0, 2))
console.log(str.substring(2, 0))
console.log(str.substring(0, 2))
console.log(str.slice(0, -1))
console.log(str.slice(-1, 0))
Output:
He
He
He
Helloworl
( empty string)
Statement: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