JavaScript in Slice (), splice (), split (), substring (), substr () How to use

Source: Internet
Author: User

1.slice ();

Both array and string objects have

Slice (I,[j]) in array

I is the index value that begins to intercept, the negative number represents the index value from the end, 1 is the first element to the bottom
J is the end of the index value, and the default is to get all elements from I to the end

Parameter returns:
Returns an array of index values from I to J, the original array does not change

In string slice (I,[j])

Parameter description:
I is the index value to start intercepting, the negative number represents the index value from the end, 1 is the first character
J is the end of the index value, and the default is to get all the characters from I to the end

2.splice ()

There is a method in array to add/remove items to/from the array, and then return the item that was deleted. The method will change the original array

Splice (INDEX,HOWMANY,ITEM1,ITEMX)

Index: Required. An integer that specifies the location of the Add/remove item, using a negative number to specify the position from the end of the array.

Howmany: Required. The number of items to delete. If set to 0, the item is not deleted.

ITEM1...ITEMX: Optional. Adds a new item to the array.

The return value array contains the new array of deleted items, if any.

3.split ()

Split in string (Separator,howmany)

Separator: Required. A string or regular expression that splits stringobject from where specified by the parameter.

Howmany: Optional. This parameter specifies the maximum length of the returned array. If this parameter is set, the returned substring will not be more than the array specified by this parameter. If this argument is not set, the entire string is split, regardless of its length.

return value

An array of strings. The array is created by splitting the string stringobject into substrings at the boundary specified by separator. The string of the returned array does not contain the separator itself

However, if separator is a regular expression that contains a subexpression, the returned array includes the strings that match those sub-expressions (but not the text that matches the entire regular expression)

Opposite effect to jion () function

4.substring ()

In string substring (start,stop)

Start: Indicates the starting position of the substring,

Stop: Indicates the end result.

Note: The second parameter should be greater than the first one. If the first argument is greater than the second argument, the substring method automatically replaces the position of the two parameter.

5.substr ()

In string, substr (start,length);

Start: The starting position of the substring,

Length: The lengths of substrings.

The above is the whole content of this article, I hope you can enjoy. Please note that it is reproduced.

JavaScript in Slice (), splice (), split (), substring (), substr () How to use

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.