Some knowledge that I can easily confuse (about array manipulation, manipulation of Strings)

Source: Internet
Author: User

1. Adding elements to an array

A. Add directly by subscript: array name [subscript]= value

score[2]=87;

B. Add by length: array name [array name. length]= value

score[score.length]=87;

C. Add by Push: array Name. push (element) (multiple elements can be added)

Score.push (87,65,77);

2. Deletion of arrays

A. Array name. length= length (directly by specifying length to determine the size of the array to achieve the effect of the Deletion)

var score=[87,65,55,77];

score.length=2;

B.pop () deletes the last element, and returns a value

var score=[87,65,55,77];

Num=score.pop ();(deleted last element score=[87,65,55])

C.shift () Deletes the first element and returns a value

var score=[87,65,55,77];

Num=score.shift ();(deleted First element score=[65,55,77])

D.splice (start position, length, Insert Element) enables the deletion, addition, modification of arrays

var score=[87,65,55,77];

Score.splice (2,2) (deleted array: score=[87,65])

Score.splice (2,0,22) (added array: score=[87,65,22,55,77])

Score.splice (2,1,91) (modified array: score=[87,65,91,55,77])

3. Sorting of arrays

A. Ascending

Score.sort ();

B. Descending

var socre1=score.sort ();

Score1.reverse (); (the array is now sorted in ascending order, then Reversed)

4. Intercepting strings

var str= "hello world";

var newstr=str.charat (4); (take the character at the corresponding position, starting from 0)

5. String Connection

var str1= "hello";

var str2= "world"

Str1.concat (str2);

6. Interception of three types of strings

a.slice (start position, End Position)

var str= "hello world";

Str.slice (0,str.length-1); (the result is: str= "hello worl");

B.substring (start position, End Position)

Str.substring (0,str.length-1); (the result is the same as above, but when there is no end position, it means that the intercept starts at the end of the String)

C.substr (start position, intercept Length) (if The start position is negative, it means backward Fetching)

note: The difference between slice () and substring (): The starting position of the slice can be negative, which means that the substring does not support negative values.

7. String Matching

A.indexof

var str= "welcome to chengdu";

Str.indexof ("e");(returns the position where the element matches, the element subscript starts at 0 and returns 1 if the string is not retrieved)

B.match

var str1=str.match ("w"); (when matching to a string, the array type is returned, and null is returned when no match is reached.) By contrast, IndexOf is faster than Match)

8. Splitting A string

var str= "hello world";

Str.split (""); (split with a space, returns an array of Strings)

9. Replace

Replace (replaced content , replace Content)

var str= "hello world";

Str=str.replace ("hello", "hi");

Some knowledge that I can easily confuse (about array manipulation, manipulation of Strings)

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.