Common methods for strings and arrays

Source: Internet
Author: User

A string Literals Common methods: 1.substring (Start position index, end end position index) The truncated position does not contain the ending position of the character, write only one parameter indicates the restart position intercept last

Input negative value becomes 0, which is smaller as the start

Str.substring (1,-3) =>str.substring (0,1);//b

Str.substing ( -1,1) =>str.substring (0,1)//b

2. Slice (Start start position index, end end position index) basic and substring similar, difference in parameter is negative.

is negative:

Returns NULL when the second argument is greater than the first argument

3. substr (Start position index, end needs to return the number of characters)

When a negative number is entered, the start parameter is added to the length of the string, and the argument becomes 0 when the end is negative.

If only one argument is passed, all subsequent

4. the CharAt (index) method returns the character at the specified index position. Returns an empty string if the valid range (0 and string length minus one) is exceeded.

5. indexof (String) returns the position of the first occurrence of a string within a string object. If no string is found, -1 is returned.

6. LastIndexOf (String) reverse lookup

Returns the corresponding index of the position of the first occurrence in a string object. If not, return-1;

Var str= ' abcdefga ' str.lastindexof (' a ')//7

7. Split (str) splits a string into an array with parameters

8. The toLowerCase method returns a string in which the letters are converted to lowercase. 9. toUpperCase10. Match ()-method to retrieve the specified value within a string, or to find the match of one or more regular expressions11. The Search () method returns the position of the first string that is used to find content with a regular expression. 12. replace () is used to find a string that matches a regular expression, and then use the new string instead of the match;Two common methods of arrays1. The Push () is added to the last returned array after the addition;2. Unshift () is added to the front to return the added array;3. Shift () Delete (removed from the previous) returns the processed array;4. Pop () Delete the last item returns the processed array5. reverse () reverses the order of elements in an array6. Join () array into a string

Var arr=[1,2,3,4,5];

Var str=arr.join ('-');

Console.log (str)//' 1-2-3-4-5 ' cuts an array with parameters within a join

Console.log (arr)//[1,2,3,4,5,6] original array unchanged

7. Slice (start,end) returns a new array from start (start) to end (end not included) in the array, the original array is unchanged

Var arr=[1,2,3,4,5];

Var Arr2=arrslice (2,4);

Console.log (ARR2)//[3,4];

Console.log (arr)//[1,2,3,4,5]

8. The concat () method is used to concatenate two or more arrays.

The method does not alter the existing array, but only returns a copy of the concatenated array.

Definition and usage

The Concat () method is used to concatenate two or more arrays.

The method does not alter the existing array, but only returns a copy of the concatenated array.

Grammar

Arrayobject.concat (Arrayx,arrayx,......, Arrayx)

Parameters

Describe

Arrayx

Necessary. The parameter can be a specific value, or it can be an array object. can be any number.

return value

Returns a new array. The array is generated by adding all the Arrayx parameters to the Arrayobject. If the argument for the concat () operation is an array, then the element in the array is added, not the array.

Instance

Example 1

In this example, we will connect the parameters in Concat () to array A:

<script type= "Text/javascript" >

var a = [n/a];

document.write (A.concat (4,5));

</script>

Output:

1,2,3,4,5

Example 2

In this example, we create two arrays and then use CONCAT () to connect them together:

<script type= "Text/javascript" >

var arr = new Array (3)

Arr[0] = "George"

ARR[1] = "John"

ARR[2] = "Thomas"

var arr2 = new Array (3)

Arr2[0] = "James"

ARR2[1] = "Adrew"

ARR2[2] = "Martin"

document.write (Arr.concat (ARR2))

</script>

Output:

George,john,thomas,james,adrew,martin

Example 3

In this example, we create three arrays and then use CONCAT () to connect them together:

<script type= "Text/javascript" >

var arr = new Array (3)

Arr[0] = "George"

ARR[1] = "John"

ARR[2] = "Thomas"

var arr2 = new Array (3)

Arr2[0] = "James"

ARR2[1] = "Adrew"

ARR2[2] = "Martin"

var arr3 = new Array (2)

Arr3[0] = "William"

ARR3[1] = "Franklin"

document.write (Arr.concat (ARR2,ARR3))

</script>

Output:

George,john,thomas,james,adrew,martin,william,franklin

9. Splice (start subscript, number, ele1,ele2 ....) ) Splicing Array

(1) A parameter from the parameter position intercept fill negative similar to the above slice back truncated array original array changes

(2) two parameters intercept (start position, number) returns the array changes of the truncated arrays

(3) Add original array increment

(4) Replacement

10. Arr.foreach () traversal, looping each of the jquery-like

Arr.foreach (funtion (Item,index,array) {

})

Where the item parameter is each item in the array, index is its indexes, and the array is the group itself

11. sort () sorting

By default the sort method is sorted in ASCII alphabetical order instead of what we think is sorted by number size

So we're going to write a way for ourselves.

Three Some common mathematical methods

Math.Abs () Take absolute value Math.ceil () rounding up Math.floor () down rounding Math.Round ()

Common methods for strings and arrays

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.