Summary of array and string methods

Source: Internet
Author: User
Tags string methods

The subject needs to change the array a=[1,2,3] into [1,2,3,4], which needs to be changed to the original array A.
The array object is commonly used in methods:
Do not change the original array:

1, concat ()
Connect two or more arrays
Do not change the original array
Returns a copy of the connected array
2. Join ()
Put all the elements in an array into a string
Do not change the original array
return string
3, Slice ()
Returns the selected element from an existing array
Do not change the original array
Returns a new array
4. ToString ()
Convert an array to a string
Do not change the original array
Returns the string form of an array
Change the original array:

5. Pop ()
Delete the last element of the array, if the array is empty, do not change the array, return undefined
Change the original array
Returns the element that was deleted
6. Push ()
Add one or more elements to the end of the array
Change the original array
Returns the length of the new array
7, reverse ()
Reverses the order of elements in an array
Change the original array
Returns the array
8, Shift ()
The first element of the array is deleted, and if an empty array is not done, it returns undefined
Change the original array
Returns the value of the first element
9. Sort ()
Sorting an array element (ASCII)
Change the original array
Returns the array
10, Splice ()
Add/Remove items from an array
Change the original array
Returns the element that was deleted
11, Unshift ()
Add one or more elements to the beginning of an array
Change the original array
Returns the length of the new array

The usage of substring and substr in JS
The substring method is used to extract the character of a string intermediary between two specified subscripts

SUBSTRING (start,end)

Start and end position, zero-based index

Parameter description
Start Required. A nonnegative integer that specifies the position of the first character of the substring to be extracted in the stringobject.
Stop is optional. A nonnegative integer that is 1 more than the last character of the substring to extract in Stringobject. If this argument is omitted, the returned substring continues until the end of the string.

return value

A new string value that contains a substring of stringobject whose contents are all characters from start to stop-1, with a length of stop minus start.

Description

The substring returned by the substring method includes the character at start, but does not include the character at end.
If start is equal to end, then the method returns an empty string (that is, a string of length 0).
If start is larger than end, then the method swaps the two parameters before extracting the substring.
If start or end is negative, it will be replaced with 0.

2.substr method

Definition and usage

The Substr method is used to return a substring of the specified length starting at the specified position.

Grammar

Stringobject.substr (start [, length])

Parameter description
Start Required. The starting position of the desired substring. The index of the first character in the string is 0.
Length is optional. The number of characters that should be included in the returned substring.

Description

If start is a negative number, then Start=str.length+start.
If length is 0 or negative, an empty string is returned.
If this argument is not specified, the substring continues to the end of the stringobject.

Example:
var str = "0123456789";

Alert (str.substring (0));------------"0123456789"
Alert (str.substring (5));------------"56789"
alert (str.substring);-----------""
alert (str.substring);-----------""
Alert (str.substring ( -5));-----------"0123456789"
Alert (str.substring ( -10));----------"0123456789"
Alert (str.substring ( -12));----------"0123456789"
Alert (str.substring (0,5));----------"01234"
Alert (str.substring (0,10));---------"0123456789"
Alert (str.substring (0,12));---------"0123456789"
Alert (str.substring (2,0));----------"01"
Alert (str.substring (2,2));----------""
Alert (str.substring (2,5));----------"234"
Alert (str.substring (2,12));---------"23456789"
Alert (str.substring (2,-2));---------"01"
Alert (str.substring ( -1,5));---------"01234"
Alert (str.substring ( -1,-5));--------""

Alert (STR.SUBSTR (0));---------------"0123456789"
Alert (STR.SUBSTR (5));---------------"56789"
alert (STR.SUBSTR);--------------""
alert (STR.SUBSTR);--------------""
Alert (STR.SUBSTR ( -5));--------------"56789"
Alert (STR.SUBSTR ( -10));-------------"0123456789"
Alert (STR.SUBSTR ( -12));-------------"0123456789"
Alert (STR.SUBSTR (0,5));-------------"01234"
Alert (STR.SUBSTR (0,10));------------"0123456789"
Alert (STR.SUBSTR (0,12));------------"0123456789"
Alert (STR.SUBSTR (2,0));-------------""
Alert (STR.SUBSTR (2,2));-------------"23"
Alert (STR.SUBSTR (2,5));-------------"23456"
Alert (STR.SUBSTR (2,12));------------"23456789"
Alert (STR.SUBSTR (2,-2));------------""
Alert (STR.SUBSTR ( -1,5));------------"9"
Alert (STR.SUBSTR ( -1,-5));-----------""

Summary of array and string methods

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.