JavaScript---Methods for string and array

Source: Internet
Author: User
Tags array length

In the course of learning JavaScript, you have encountered a lot of operations on arrays and strings. A lot of methods have been summed up before, but when I met my own use, I forgot. Do not forget the method called what name, just forget the method parameters have what, what is returned?

Now let's summarize it again:

The first is the method of the string:

1. Character Method:

CHARAT ()---Function: Returns the character at the specified position; syntax: String.charat (index) index out of range returns an empty string

  

1     var str = ' Hello '; 2     var n = str.charat (1); 3     Console.log (n);//return character E

        

charCodeAt ()---Function: Returns the Unicode encoding of the character at the specified position; syntax: string.charcodeat (index) index out of range return Nan

1     var str1 = ' Hello '; 2     var n1 =  str1.charcodeat (1); 3     Console.log (N1);//Return 101

fromCharCode ()---Function: Accepts the specified Unicode value, returns the corresponding string; syntax: String.fromCharCode (); parameter: N1,n2,... NX

1     var n2 = String.fromCharCode (72,69,76,76,79); 2     Console.log (n2);//Return HELLO

2. Location Method:

INDEXOF ()---Function: Returns the index position of the first occurrence of a specified string, Syntax: String.IndexOf (Searchvalue,start) Searchvalue is a required value, start is optional, specifying the position at which to start the index,  The default is 0, the return value: The first occurrence of the string where number is searched, and 1 if no matching string is found. Degree of Use: * * *

  

1                 var str3= "Hello World, Welcome to the universe"; 2         var n3=str3.indexof ("E", 5); 3         Console.log (n3);//Return 14        

LASTINDEXOF ()---Function: Returns the position of the last occurrence of a specified string value, which is searched from behind at the specified position in a string. Syntax: String.IndexOf (Searchvalue,start) Searchvalue is a required value, start is optional, specifies the position at which to start the index; the return value: The last occurrence of the string where number is found, or 1 if no matching string is located.

1             var str4= "Hello planet Earth, you is a great planet."; 2         var n4=str4.lastindexof ("Planet"); 3         Console.log (N4);//Return 36    

3. Matching method:

Match ()----English: Matching function: Retrieves the specified substring within a string, or finds a match for one or more regular expressions. Syntax: String.match (regexp); return value: Array

Note: The return value is an array.

12var n5=str5.match (/ain/gi); 3 Console.log (N5);//Return ["Ain", "Ain", "Ain", "Ain"]

Search ()---function: Used to retrieve a substring specified in a string, or to retrieve a substring that matches a regular expression, or 1 if no matching substring is found; syntax: String.search (searchvalue) Searchvalue required A string or regular expression to find. ; return value: Number

1 var str6= "Mr Blue has a Blue house"; 2 var N6 = (Str6.search ("Blue")); 3 Console.log (N6);//Return 15

Split ()---English: cutting function: Used to split a string into substrings. And then make up the array. Syntax: String.Split (separator,limit). Separator (delimiter) optional parameter selected to split the string limit is optional, which specifies the maximum length of the returned array. If this parameter is set, the returned substring will not be more than the array specified by this parameter. If this argument is not set, the entire string is split, regardless of its length. Return value: Array usage: *************

1        //Omit split parameters2 var str7= "How is you doing today?";3 var n7=str7.split ();4 Console.log (N7);//Return ["How is you doing today?"] Array length is 15 //' splits all characters, including spaces6 var n8=str7.split (');7 Console.log (N8);//["H", "O", "w", "", "a", "R", "E", "" "," Y "," O "," U "," "," D "," O "," I "," N "," G "," "," T "," O "," D "," a "," Y ","? "]8 //Use the limit parameter9 var n9=str7.split (', 3);Ten Console.log (N9);//Return ["How", "is", "You"] One //Use a character as a delimiter A var n10=str7.split (' O '); -Console.log (N10);//Return ["H", "W is y", "U d", "ing t", "Day"]

    

4. How to Operate

Stitching method concat ()---Function: Used to concatenate two or more strings; syntax: String.Concat (string1, string2, ..., stringx) string1, string2, ..., stringx required, One or more string objects that will be concatenated as a string. The return value: A string that is generated after two or more strings are concatenated.

 

1 var str8= "Hello"; 2         var str9= "world!"; 3         var str10= "has a nice day!"; 4         var n11 = Str8.concat (STR9,STR10); 5         Console.log (N11);//return string ' Hello world! Has a nice day! '      

      

Interception method Slice () English: Slice function: Extracts a part of a string and returns the extracted part with a new string; Syntax: String.slice (start,end) Start must. The starting subscript of the fragment to extract. The first character position is 0 end optional. The subscript immediately following the end of the fragment to be extracted. If this parameter is not specified, the substring to be extracted includes the string from start to the end of the original string. If the parameter is negative, it specifies the position from the end of the string. return value: String usage: ********

1       //Extract all Strings2 var str11= "Hello world!";3 var n12=str11.slice (0);4 Console.log (N12);//return ' Hello world! '5 //Extract a string from the 3rd position of the string6 var n13=str11.slice (3);7 Console.log (N13);//Return ' lo world! '8 //Extract a string from the 3rd position of the string to the 8th position9 var n14=str11.slice (3,8);Ten Console.log (N14);//Return ' lo wo ' One //Extract last character A var n15=str11.slice ( -1); -Console.log (N15);//return '! '

SUBSTRING () English: substring function: Extracts the character of a string intermediary between two specified subscripts, excluding the subscript at the end; syntax: String.substring (from,to) from required, a non-negative integer. To optional. A nonnegative integer that, if omitted, returns the substring until the end of the string. return value: String usage: *******

1 var str12= "Hello world!"; 2         document.write (str12.substring (3) + "<br>");//Return ' lo world! ' 3         document.write (str12.substring (3,7));//Return ' Lo w '

SUBSTR ()---Function: Extracts the specified length of characters from the subscript in a string; Syntax: String.substr (START,LENGTH) star required. The starting subscript for the substring to extract. Must be numeric. If it is a negative number, the argument declares the position from the end of the string. That is,-1 refers to the last character in the string, 2 refers to the second-lowest character, and so on. Length is optional. The number of characters in the substring. Must be numeric. If this argument is omitted, the string from the beginning of the stringobject to the end is returned. return value: String

1 var str13= "Hello world!"; 2         var n16=str13.substr (2); 3         Console.log (N16);//' Llo world! '

  

Summarize the method of the string, and then summarize the method of the array:

        

JavaScript---Methods for string and array

Related Article

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.