How to Use slice (), splice (), split (), substring (), and substr () in javascript _ javascript tips

Source: Internet
Author: User
This article mainly introduces how to use slice (), splice (), split (), substring (), and substr () in javascript. For more information, see 1. slice ();

Both Array and String objects have

Slice (I, [j]) in Array

I is the index value that starts to be intercepted. a negative number indicates the index value counted from the end.-1 is the last element.
J is the end index value. if j is left blank, all elements from I to the end are obtained.

Parameter return:
Returns an array of index values from I to j. the original array is not changed.

Slice (I, [j]) in String

Parameter description:
I is the index value that starts to be truncated. a negative number indicates the index value counted from the end.-1 is the first to last character.
J is the end index value. if it is short of time, all characters from I to the end are obtained.

2. splice ()

An Array method exists to add/delete a project to/from the Array, and then return the deleted project. This method will change the original array

Splice (index, howmany, item1, itemx)

Index: required. Integer that specifies the position of the added/deleted project. a negative number can be specified from the end of the array.

Howmany: required. The number of projects to delete. If it is set to 0, the project will not be deleted.

Item1. .. itemX: Optional. Add a new project to the array.

The returned Array contains the new Array of the deleted item, if any.

3. split ()

Split (separator, howator) in String)

Separator: required. String or regular expression, which separates stringObject from the specified place.

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

Return value

A string array. This array is created by dividing 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 contains the strings that match the subexpression (but does not include the text that matches the entire regular expression)

Opposite to the jion () function

4. substring ()

Substring (start, stop) in String)

Start: the starting position of the substring,

Stop: indicates the end result.

Note: The second parameter must be greater than the first parameter. If the first parameter is greater than the second parameter, the substring method automatically replaces the positions of the two parameters.

5. substr ()

In String, substr (start, length );

Start: the starting position of the substring,

Length: the length of the substring.

The above is all the content of this article. I hope you will like it.

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.