JavaScript string Object substring method Getting started instance (for intercepting strings) _ Basics

Source: Internet
Author: User

JavaScript substring method

The substring method is used to intercept a string by specifying a start and end position and to return an intercept part of the string. The syntax is as follows:

Copy Code code as follows:

Str_object.substring (start, end)

Parameters Description
Str_object The string to manipulate (object)
Start Necessary. Start intercept position, nonnegative integer
End Optional. The position of the end of the string interception, a nonnegative integer, or, if omitted, until the end of the string

Tip: If the parameter start is equal to end, then the method returns an empty string. If start is larger than end, the method exchanges the two parameters before intercepting the string.

Substring Method Instance

Copy Code code as follows:

<script language= "JavaScript" >

var str = "ABCdef";
document.write (str.substring (1, 3) + "<br/>");
document.write (Str.substring (2, 2) + "<br/>");
document.write (Str.substring (3, 1));

</script>




Run this example, output:


Copy Code code as follows:

Bc

Bc

The difference between substring and slice and substr

The 1.substring cannot accept negative parameters, and the slice method can
2.substring The second parameter is the position at which the end of the interception is specified, and the second parameter of the Substr method is the length of the specified string interception

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.