JS Basics 1

Source: Internet
Author: User
Tags truncated

Array

  Create an array

    

var New Array (); var New Array (3); var New Array (' JavaScript ', ' Java ', ' PHP ');

Adding an array element

Arr.push (); arr[0] = ' Redis ';

To delete an array element

   1.delete method: Delete Arr[1]
This way the array length is not changed, at this point arr[1] becomes undefined, but also has the benefit of the original array index also remains unchanged, at this time to go through the group elements can be used
For (index in ARR)
document.write (' arr[' +index+ ']= ' +arr[index]);
This traversal way skips the elements of undefined

* This method IE4.O support after all.

2. Array Object Splice method: Arr.splice (n);
This way the array length changes accordingly, but the original array index also changes accordingly
The first 1 in the splice parameter is the starting index of the deletion (from 0), where the second element of the array
The second 1, is to delete the number of elements, here only one element, that is, ' B ';
Traversing an array element can be used to iterate through an array, such as for, because the deleted element
The array does not retain

* This method is only supported after IE5.5

It is worth mentioning that the splice method can also add elements to an array while deleting an array element
For example, Arr.splice (' d ', ' e '), d,e two elements are added to the array arr.
The resulting array becomes arr: ' A ', ' d ', ' e ', ' C

String splitting

// This is a string var New // define an array // Character Segmentation

String interception

  Slice (Start,[end])

    Start: Starting position

End: The next position of the end position

The length of the truncated string is the difference between the second parameter and the first parameter, and if the parameter value is negative, the value is added to a positive number after the string length, or an empty string if the first parameter equals greater than the second argument.

  SUBSTRING (Start,[end])

    Start: Starting position

End: The next position of the end position

If the value of the parameter is negative, the value is converted to 0, and the two parameter takes a smaller value as the starting position, and the length of the truncated string is the difference between the larger value and the smaller value.

  SUBSTR (Start,[length])

    Start: Starting position

Length: Size of the truncated string

PS: Strings are counted starting from 0

  The difference between substr and substring methods

varstr = "0123456789";//Alert (str.substring (0));//------------"0123456789"Alert (str.substring (5));//------------"56789"Alert (str.substring (10));//-----------"" Alert (str.substring (12));//-----------"" 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));//----------" the"Alert (str.substring (2,2));//----------"" Alert (str.substring (2,5));//----------"234"Alert (str.substring (2,12));//---------"23456789"Alert (str.substring (2,-2));//---------" the"Alert (str.substring ( -1,5));//---------"01234"Alert (str.substring ( -1,-5));//--------"" Alert (STR.SUBSTR (0));//---------------"0123456789"Alert (STR.SUBSTR (5));//---------------"56789"Alert (STR.SUBSTR (10));//--------------"" Alert (STR.SUBSTR (12));//--------------"" Alert (STR.SUBSTR (-5));//--------------"0123456789"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));//-------------"All"Alert (STR.SUBSTR (2,5));//-------------"23456"Alert (STR.SUBSTR (2,12));//------------"23456789"Alert (STR.SUBSTR (2,-2));//------------"" Alert (STR.SUBSTR ( -1,5));//------------"01234"Alert (STR.SUBSTR ( -1,-5));//-----------"" 

Array composition string

var delimitedstring=Myarray.join (delimiter); var mylist=New  Array ("JPG", "BMP", "GIF", "ico", "PNG"); var portablelist=mylist.join ("|"); // The result is jpg|bmp|gif|ico|png .

Get the string subscript

var mystring="JavaScript"; var w=mystring.indexof ("V"); W'll be 2var x=mystring.indexof ("S"); x would be 4var y= Mystring.indexof ("Script"); y'll also be 4var z=mystring.indexof ("key"); Z'll be-1

JS Basics 1

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.