JavaScript Arrays and String basics

Source: Internet
Author: User

Some common usages and exercises of arrays

Array.concat () connection array

Practice:

var arr=[1,2,3,4,5];
ARR1=[7,8,9];
document.write (Arr.concat (arr1[0))//Return 1,2,3,4,5,7
Array.join () joins the array elements to construct a string.

The size of the Array.Length array

Array.pop () deletes and returns the last element of the array

Practice:
var arr=[1,2,3,4,5]
document.write (Arr.pop ());//Return 5
document.write ("</br>" +arr);//Return 1,2,3,4

Array.push () Adding elements to an array

Array.reverse () Reverses the order of the elements in the array
Practice:
var arr=[1,2,3,4,5]
Arr.reverse ();
document.write (arr); return 5,4,3,2,1

Array.shift () moves an element to an array
Practice:
var arr=[1,2,3,4,5];
Arr.shift ();
document.write (arr); return 2,3,4,5

Array.slice () returns part of an array

Practice:

var arr=[1,2,3,4,5];
document.write (Arr.slice (0,3)); return to

Array.Sort () sorting an array element

Array.splice () Inserting, deleting, or replacing elements of an array

Practice:

Syntax: Array.splice (Start, DeleteCount, value, ...)

var arr=[1,2,3,4,5];
ARR1=[7,8,9];
Arr.splice (0,2,12,23);
document.write (arr);//Return 12,23,3,4,5


Array.tolocalestring () converts an array to a local string

Array.tostring () converts an array to a string

Array.unshift () Inserts an element at the head of the array

some common usages of string practice

String.charat () returns the nth character in a string
Practice:
var str= "Gagtrhst";
Str.charat (2); return g
String.charcodeat () code that returns the nth character in a string

String.Concat () connection string

String.fromCharCode () Create from character encoding-a string

String.IndexOf () Retrieving strings

String.LastIndexOf () Retrieves a string from the back forward
Practice:

var str= "Gagtrhst";
Str.lastindexof ("HST" [2]); return 7
String.Length Length of string

String.localecompare () compares two strings in a local-specific order

String.match () finds the match of one or more regular expressions

String.Replace () replaces a substring that matches a regular expression
Practice:

var str= "Gagtrhst";
document.write (Str.replace ("G", ' a ')); Replace the first G with a

String.search () Retrieves a substring that matches a regular expression
Practice:
var s = "JavaScript is fun";
document.write (S.search (/script/i)); Returns the position of the first character of the first matched substring

String.slice () Extracts a child
Practice:
var s = "ABCDEFG";
S.slice (0,4)//Return to "ABCD"

String.Split () splits a string into a string array
Practice:
var str= "Gagtrhst";
document.write (Str.split ("", 3)); return g,a,g
"1:2:3:4:5". Split (":"); return ["1", "2", "3", "4", "5"]

STRING.SUBSTR () Extracts a substring
Practice:

var str= "Gagtrhst";
Alert (STR.SUBSTR (2,2)); return to GT
String.substring () returns a substring of a string

String.tolocalelowercase () Converts a string to lowercase

String.tolocaleuppercase () Converts a string to uppercase

String.tolowercase () Converts a string to a small

JavaScript Arrays and String basics

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.