This article analyzes the slice() method of String object in Javascript in more detail. Share it for your reference. The specific analysis is as follows:
This method intercepts a segment in the string and returns a new string composed of the intercepted characters.
Note: The original string will not change, the return value is a newly generated string.
Syntax structure:
The code copy is as follows: stringObject.slice(start, end)
Parameter list:
| parameter | describe |
| start | Required. Specifies where to start intercepting strings. The position of the first character of the string is 0. If this parameter is negative, the position will be calculated from the end of the string. For example: -1 represents the penultimate character, -2 represents the penultimate character, and so on. |
| end | Optional. Specifies where to end the string intercept. If this parameter is omitted, all characters starting from the start position to the end will be intercepted. Note: The characters corresponding to end will not be intercepted. |
Example code:
Example 1:
The code copy is as follows: var a="abcdefgmnlxyz";
console.log(a.slice(2,3));
The string between position "2" and position "3" is intercepted, but the character d corresponding to position "3" is not within the intercept return. Output result:c.
Example 2:
The code copy is as follows: 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.