JavaScript subString () method and slice () method

Source: Internet
Author: User

Web page Effects subString () method and slice () method

The slice () method extracts a part of the string and returns the extracted part with a new string.
Grammar
Stringobject.slice (Start,end)
Parameter description
The starting subscript of the fragment to be extracted by start. If it is a negative number, the parameter sets the position from the tail of the string. In other words,-1 refers to the last character of the string,-2 to the penultimate character, and so on.
End is immediately followed by the bottom of the fragment to be extracted. If this parameter is not specified, the substring to be fetched includes the string at the end of the original string. If the argument is a negative number, it sets the position from the tail of the string.
return value
A new string. Includes all characters for the string stringobject from start (including start) to end ending (excluding ends).

Example 1
In this case, we'll extract all the characters starting at position 6:

<script type= "Text/javascript" >

var str= "Hello Happy world!"
document.write (Str.slice (6))

</script>


Output:
Happy world!
Example 2
In this case, we'll extract all the characters from position 6 to position 11:

<script type= "Text/javascript" >

var str= "Hello Happy world!"
document.write (Str.slice (6,11))

</script>

Substring

The substring () method extracts characters from a string mediation between two specified subscripts.
Grammar
Stringobject.substring (Start,stop)
Parameter description
Start Required. A non-negative integer that stipulates the position of the first character of the substring to be extracted in the stringobject.
Stop
Optional. A non-negative integer that is 1 more than the last character of the substring to extract in the Stringobject.
If this argument is omitted, the returned substring continues to the end of the string.
return value
A new string that contains a substring of stringobject whose contents are all characters from the start to the stop-1, whose length is stop minus start.
Description
The substring returned by the substring () method includes the character at start, but does not include the character at the end.
If the argument start is equal to end, then the method returns an empty string (that is, the length 0). If start is larger than end, the method swaps the two parameters before extracting the substring.
Tips and comments
Important: Unlike the slice () and substr () methods, substring () does not accept negative parameters.
Instance
Example 1
In this case, we'll use substring () to extract some characters from the string:

<script type= "Text/javascript" >

var str= "Hello world!"
document.write (Str.substring (3))

</script>


Output:
Lo world!
Example 2
In this case, we'll use substring () to extract some characters from the string:

<script type= "Text/javascript" >

var str= "Hello world!"
document.write (str.substring (3,7))

</script>


Output:
Lo W

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"

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.