slice of method analysis of String objects in javascript
- 2020-05-07 19:14:25
- OfStack
This paper analyzes slice() method of String object in javascript in detail. Share with you for your reference. Specific analysis is as follows:
This method intercepts a segment of a string and returns a new string of intercepted characters.
Note: the original string will not change. The return value is 1 new string.
syntax structure:
stringObject.slice(start,end)
parameter list:
参数 | 描述 |
start | 必需。规定从何处开始截取字符串。字符串的首字符的位置是0。 如果此参数为负数,那么将从字符串的尾部开始计算位置。例如:-1代表倒数第1个字符,-2代表倒数第2个字符,以此类推。 |
end | 可选。规定在何处结束截取字符串。 如果省略该参数,那么将截取从start位置开始到结尾的所有字符。 注:end对应的字符不会被截取。 |
instance code:
Example 1:
var a="abcdefgmnlxyz";
console.log(a.slice(2,3));
Intercepts a string between the positions "2" and "3", but the character d corresponding to the position "3" is not included in the intercept return. Output :c.
Example 2:
var a="abcdefgmnlxyz";
console.log(a.slice(2));
If the second argument is omitted, all characters from position "2" to the end of the string are intercepted. Output :cdefgmnlxyz.
I hope this article has been helpful to your javascript programming.