The substring () method and the slice () method of the String class in JavaScript _javascript techniques

Source: Internet
Author: User
In section 2.8.4, the substring () method and the slice () method in the string class are basically the same as the return results, as in the following example:
Copy Code code as follows:

var strobj = new String ("Hello World");
Alert (Strobj.slice (3)); Output result: "Ol World"
Alert (strobj.substring (3)); Output result: "Ol World"
Alert (Strobj.slice (3, 7)); Output result: "Lo w"
Alert (strobj.substring (3,7)); Output result: "Lo w"

The output from the above code can be seen, the slice () method and the substring () side invoke the method and the output exactly the same, both of which return a substring of the string to be processed, accepting one or two arguments, and the first parameter is the starting position of the substring to get. The second argument is to get the ending position of the substring, which defaults to the length of the string if the second argument omits the end position, and two methods do not change the value of the string object itself.

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

For negative arguments, the slice () method uses the length of the string and the SubString () method to treat it as 0, for example:
Copy Code code as follows:

var strobj = new String ("Hello World");
Alert (Strobj.slice (-3)); Output result: "Rld"
Alert (Strobj.substring (-3)); Output result: "Hello World"
Alert (Strobj.slice (3,-4)); Output result: "Lo w"
Alert (strobj.substring (3,-4))//Output result: "Hel"

This will see the major differences between the slice () and substring () methods. When there is only argument-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) is converted to substring (0). Similarly, using the 3 and-4 differences is also obvious. The slice () method is converted to slice (3,7), the same as the previous example, and returns "Lo W". The SubString () method interprets the two parameters as SubString (0,3), which is actually: subString (0,3), because subString () always takes the smaller argument as the starting bit and the larger number terminates the bit.

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.