Slice () method analysis of String objects in javascript, stringslice
This article analyzes in detail the slice () method of the String object in javascript. Share it with you for your reference. The specific analysis is as follows:
This method intercepts a portion of a string and returns a new string consisting of the truncated characters.
Note: The original string does not change. The returned value is a new string.
Syntax structure:
Copy codeThe Code is as follows: stringObject. slice (start, end)
Parameter List:
| Parameters |
Description |
| Start |
Required. Specifies where to start intercepting strings. The first character of the string is 0. If this parameter is negative, the position is calculated from the end of the string. For example,-1 indicates the first to last,-2 indicates the second to last, and so on. |
| End |
Optional. Specifies where to end string truncation. If this parameter is omitted, all characters starting from start and ending are truncated. Note: The end character is not truncated. |
Instance code:
Instance 1:
Copy codeThe Code is as follows: var a = "abcdefgmnlxyz ";
Console. log (a. slice (2, 3 ));
Truncate the string between "2" and "3", but the character d corresponding to "3" is not included in the return result. Output result: c.
Example 2:
Copy codeThe Code is as follows: var a = "abcdefgmnlxyz ";
Console. log (a. slice (2 ));
If the second parameter is omitted, all characters starting from "2" to the end of the string are truncated. Output result: cdefgmnlxyz.
I hope this article will help you design javascript programs.