JavaScript slice (), splice (), split (), substring (), substr () use method _javascript tips

Source: Internet
Author: User

1.slice ();

Both array and string objects have

Slice in Array (I,[j])

I is the index value that starts the interception, and the negative number represents the index value from the end, minus 1 is the first element.
J is the ending index value, and the default is to get all elements from I to the end

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

Slice in string (I,[j])

Parameter description:
I is the index value that starts the interception, and the negative number represents the index value from the end, minus 1 is the first character
J is the ending index value, and the default is to get all characters from I to the end

2.splice ()

There is a method in array that adds/deletes items to/from an array, and then returns the deleted item. This method will change the original array

Splice (INDEX,HOWMANY,ITEM1,ITEMX)

Index: Required. Integer that stipulates where to add/remove items, and use negative numbers 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. The new item to add to the array.

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

3.split ()

Split in string (Separator,howmany)

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

Howmany: Optional. This parameter specifies the maximum length of the array to return. If this argument 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 separator itself

However, if separator is a regular expression that contains a subexpression, the returned array includes a string that matches the subexpression (but does not include text that matches the entire regular expression)

The opposite effect of the jion () function

4.substring ()

Substring in string (start,stop)

Start: Represents the starting position of the substring,

Stop: Indicates the end result.

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

5.substr ()

In string, substr (start,length);

Start: The starting position of the substring,

Length: The lengths of substrings.

The above is the entire content of this article, I hope you can enjoy.

Related Article

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.