Access the substring of a string in JavaScript.

Source: Internet
Author: User

Access the substring of a string in JavaScript.

Use the substring () or slice () method (NN4 +, IE4 +). The following describes their usage.


The prototype of substring () is:

string.substring(from, to)


The first parameter from specifies the starting position of the substring in the original string (based on the 0 index); the second parameter to is optional, it specifies the substring at the end of the original string (based on the 0 Index). In general, it should be larger than from. If it is omitted, then the substring ends at the end of the original string.


What if the from parameter is bigger than the parameter? JavaScript will automatically mediate the starting and ending positions of the substring, that is, the substring () always starts from the smaller of the two parameters and ends with the larger one. Note that it contains the character at the starting position, but does not contain the character at the ending position.

Var fullString = "Every dog has his day. "; var section = fullString. substring (0, 4); // section is "Ever ". section = fullString. substring (4, 0); // section is also "Ever ". section = fullString. substring (1, 1); // section is an empty string. section = fullString. substring (-2, 4); // section is "Ever", same as fullString. substring (0, 4); the prototype of slice () is: string. slice (start, end)



The start parameter indicates the starting position of the substring. If it is a negative number, it can be understood as the last few, for example,-3 indicates starting from the last three; the end parameter indicates the end position, like start, it can also be a negative number, which means that the last few ends. Slice () parameters can be negative, so it is more flexible than substring (), but not so tolerant. If start is larger than end, it returns an empty string (for example, omitted ).


Another method is substr (). Its prototype is:

string.substr(start, length)


From the prototype, we can see the meaning of its parameters. start indicates the starting position, and length indicates the length of the substring. This method is not recommended for JavaScript standards.

Related Article

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.