JavaScript slice character intercept function usage

Source: Internet
Author: User

Slice usage

String.slice (NUM1, num2)
String.slice (num)

The slice () method extracts a part of the string and returns the extracted part with a new string.

return value
A new string. Includes all characters for the string stringobject from start (including start) to end ending (excluding ends).

Instance

<script language= "JavaScript" >
<!--
var myString = new String ("This is a Test");

var myslice = Mystring.slice (0,6);

document.write (' The ' the ' the ' the ' 7 characters our string, ' + myString ');
document.write (', are: ' + myslice);
Document.close ();

-->
</script>


Note: You can use negative values to select elements from the tail of the array.

Note: If end is not specified, then the slice () method selects all elements from start to ending of the array.

Instance

<script type= "text/web Effects" >

var arr = new Array (3)
Arr[0] = "George"
ARR[1] = "John"
ARR[2] = "Thomas"

document.write (arr + "<br/>")
document.write (Arr.slice (1) + "<br/>")
document.write (arr)

</script>

Output

George,john,thomas
John,thomas
George,john,thomas

In this example, we will create a new array and then display the elements selected from it:

<script type= "Text/javascript" >

var arr = new Array (6)
Arr[0] = "George"
ARR[1] = "John"
ARR[2] = "Thomas"
ARR[3] = "James"
ARR[4] = "Adrew"
ARR[5] = "Martin"

document.write (arr + "<br/>")
document.write (Arr.slice (2,4) + "<br/>")
document.write (arr)

</script>

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.