JavaScript Slice string slicing function

Source: Internet
Author: User

In JavaScript we can get any part of a string through the slice function, and the slice function needs to pass in two parameters P1 and P2. P1 specifies the starting position of the substring, p2 specifies the length of the string to intercept. Note: The position of the first character in JavaScript is 0. The first example is that we want to intercept the main string of 10 characters starting at 0, so we can use slice (0,10)

In JavaScript we can get any part of a string through the slice function, and the slice function needs to pass in two parameters P1 and P2. P1 specifies the starting position of the substring, p2 specifies the length of the string to intercept. Note: The position of the first character in JavaScript is 0. The first example is that we want to intercept the main string from 0 to 10 characters, so we can use slice (0,10). The following example intercepts 7 characters from the beginning of a string
1 var my_str= "Welcome to Www.ty300.com";
2 var my_slice=my_str.slice (0,7);
3 document.write (My_slice);

The return result of this code is: Welcome.

We can pass a negative number to the parameter P2, which means let the slice function delete the last n characters of the string to see a JS code:
1 var my_str= "Welcome to Www.sharejs.com"
2 var my_slice=my_str.slice (0,-7);
3 document.write (My_slice);

The above JS code output is: Welcome to Qkxue.net,

You must have found out that the last 7 characters were deleted (sliced).

Manuscripts: Diligent Learning qkxue.net

Read the full version of JavaScript slice string slicing function

JavaScript Slice string slicing function

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.