Substring of strings in JavaScript _ basics

Source: Internet
Author: User

Using the substring () or slice () method (nn4+, ie4+), the specific use of them is described below.


The prototype for substring () is:

String.substring (from, to)


The first argument from specifies the starting position of the substring in the original string (based on the 0 index); the second argument to is optional, which specifies the substring at the end of the original string (based on the 0 index), and generally, it should be larger than from, if it is omitted, The substring is then kept at the end of the original string.


What happens if the parameter from is accidentally larger than the parameter? JavaScript automatically mediates the start and end of a substring, which means that substring () always starts with the smaller of the two arguments, and ends with the larger one. Note, however, that it contains that character from the starting position, but does not contain the character at the end position.

   var fullstring = "Every dog has his day."


   var section = fullstring.substring (0, 4); The section is "Ever".


   Section = fullstring.substring (4, 0);   The section is also "Ever".


   Section = fullstring.substring (1, 1);   The section is a 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 parameter start indicates the starting position of the substring and, if it is a negative number, it can be understood as the beginning of the penultimate, for example, 3, starting at the last third, and the argument end representing the ending position, which, like start, can also be negative, and its meaning to the penultimate end. The slice () parameter can be negative, so it is more flexible than substring (), but less forgiving, and if start is larger than end, it returns an empty string (example).


Another method is substr (), whose prototype is:

String.substr (start, length)


From the prototype, you can see the meaning of its arguments, start represents the starting position, and length represents the substring. JavaScript standards do not encourage the use of this method.

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.