This article mainly introduces the slice () method of the String object in javascript, and analyzes the definition, parameters, and specific usage of the slice () method in the form of examples, which has some reference value, for more information, see 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:
The 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:
The 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:
The 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.