Slice () method analysis of String objects in javascript _ javascript skills

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.