The difference between the string object slice () method and the substring () in JS

Source: Internet
Author: User

Slice () and substring ()

ECMAScript provides two ways to create string values from substrings, namely slice () and substring (). Both of these methods return substrings of the string to be processed and accept one or two parameters. The first parameter is the starting position of the substring to get, and the second argument (if used) is the position to get before the substring terminates (that is, the character at the end of the fetch is not included in the returned value). If you omit the second argument, the terminating bit defaults to the length of the string.

As with the concat () method, neither the slice () and the substring () methods change the value of the String object itself. They return only the original string value, keeping the string object intact.

var ostringobject = new String ("Hello World"), Alert (Ostringobject.slice ("3"));//Output "Lo World" alert ( Ostringobject.substring ("3"));//Output "Lo World" alert (Ostringobject.slice ("3", "7");//Output "Lo w" alert ( Ostringobject.substring ("3", "7"));//Output "Lo w"

In this example, slice () and substring () use the same, and the return value is the same. When only argument 3 o'clock is available, two methods return "Lo World" because the second "L" in "Hello" is on position 3. When there are two parameters "3" and "7", the value returned by the two methods is "Lo W" (The Letter "O" in "World" is located on position 7, so it is not included in the result).

Why are there two ways to function exactly the same way? In fact, these two methods are not exactly the same, but only when the parameters are negative, they handle the arguments in slightly different ways.

For a negative parameter, the slice () method adds a parameter to the length of the string, and the substring () method handles it as 0 (that is, ignores it). For example:

var ostringobject = new String ("Hello World"), Alert (Ostringobject.slice ("3")),//Output "Rld" alert ( Ostringobject.substring ("3"));//output "Hello World" alert (Ostringobject.slice ("3,-4"));//Output "Lo w" alert ( Ostringobject.substring ("3,-4"));//output "hel"

This shows the main differences between the slice () and the substring () methods.

When the parameter is only 3, slice () returns "Rld", and substring () returns "Hello World". This is because for the string "Hello World", Slice ("3") will be converted to slice ("8"), while substring ("3") will be converted to substring ("0").

Similarly, when using Parameters 3 and 4, the difference is also obvious. Slice () will be converted to slice (3, 7), as in the previous example, returning "Lo W". The substring () method interprets two parameters as substring (3, 0), which is actually substring (0, 3), because substring () always takes the smaller number as the starting bit and the larger number as the terminating bit. Therefore, substring ("3,-4") returns "Hel". The last line of code here is used to illustrate how to use these methods.

The difference between the string object slice () method and the substring () in JS

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.