Home > Article > Web Front-end > How to use slice(), splice(), split(), substring(), substr() in javascript_javascript skills
1.slice();
Both Array and String objects
In Array slice(i,[j])
i is the index value to start intercepting, the negative number represents the index value from the end, -1 is the first element from the last
j is the end index value. By default, all elements from i to the end are obtained
Parameter return:
Returns an array with index values from i to j, the original array does not change
slice(i,[j])
in StringParameter description:
i is the index value to start interception, a negative number represents the index value from the end, -1 is the first character from the last
j is the end index value. By default, all characters from i to the end
2.splice()
Exists in Array Methods add/remove items to/from the array, and then return the deleted item. This method will change the original array
splice(index,howmany,item1,itemx )
index : required. An integer specifying the position at which to add/remove an item. Use 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, items will not be deleted.
item1...itemX : Optional. New items added to the array.
Return Value Array A new array containing the deleted items, if any.
3.split()
split(separator, howmany) in String
separator: required. A string or regular expression to split the stringObject from where specified by this parameter.
howmany: Optional. This parameter specifies the maximum length of the returned array. If this parameter is set, no more substrings will be returned than the array specified by this parameter. If this parameter is not set, the entire string will be split regardless of its length.
Return value
An array of strings. The array is created by splitting the StringObject into substrings at the boundaries specified by separator. The string of the returned array does not contain the separator itself
However, if separator is a regular expression containing subexpressions, then the returned array includes strings matching those subexpressions (but not text matching the entire regular expression)
The opposite effect of 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 larger than the first parameter. If the first parameter is greater than the second parameter, the substring method will automatically replace 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 the entire content of this article, I hope you all like it.