Home > Article > Web Front-end > Analysis of slice() method of String object in javascript
This article analyzes the slice() method of String object in JavaScript in more detail. Share it with everyone for your reference. The specific analysis is as follows:
This method intercepts a section of the string and returns a new string composed of the intercepted characters.
This article mainly introduces the slice() method of String object in JavaScript. It analyzes the definition, parameters and specific usage of the slice() method in the form of examples. It has certain reference value. Friends who need it can Please refer to the following
Note: The original string will not change, and the return value is a newly generated string.
Syntax structure:
stringObject.slice(start,end)
Parameter list:
参数 | 描述 |
start | 必需。规定从何处开始截取字符串。字符串的首字符的位置是0。 如果此参数为负数,那么将从字符串的尾部开始计算位置。例如:-1代表倒数第一个字符,-2代表倒数第二个字符,以此类推。 |
end | 可选。规定在何处结束截取字符串。 如果省略该参数,那么将截取从start位置开始到结尾的所有字符。 注:end对应的字符不会被截取。 |
Example code:
Example 1:
var a="abcdefgmnlxyz"; console.log(a.slice(2,3));
Intercept the string between position "2" and position "3", but the character d corresponding to position "3" is not included in the interception return. Output result: c.
Example 2:
var a="abcdefgmnlxyz"; console.log(a.slice(2));
If the second parameter is omitted, all characters from position "2" to the end of the string will be intercepted. Output result: cdefgmnlxyz.
I hope this article will be helpful to everyone’s JavaScript programming design. For more related tutorials, please visit JavaScript Video Tutorial!