Mutual conversion of strings and arrays in JavaScript

Source: Internet
Author: User
Tags javascript array

Mutual conversions of strings and arrays in javascript:
The conversion of strings and arrays is very important, because in the actual coding process will often use, so this is the knowledge point that must be mastered, of course, this knowledge point is not difficult, know that always know, not the kind of things that need to be fully practiced to grasp, the following to do a brief introduction.
I. String conversions to arrays:
This operation uses the split () function, which converts the string to an array with the specified character as a delimiter, and the instance code is as follows:

var Str= "Abc-mng-zhang-mayi"; var newarray=str.split ("-"); Console.log (NewArray);

As can be seen from the output, the split () function has converted the string into an array.
Two. Convert the array to a string:
This can be done using the join () function of the array object, which connects the elements in the array with the specified characters and returns the resulting string.
The code is as follows:

var newarray=["abc", "Mng", "Zhang", "Mayi"]; var Str=newarray.join ("-"); Console.log (STR);

The above code implements our requirements by using "-" to concatenate the array elements and generate a string.
The split () function can refer to the section of the Split () method of the JavaScript string object .
The join () function can refer to the section of the join () method of the JavaScript array object .
The two examples above are the functions that are used, and of course we can write them ourselves, which is more flexible and know everything.
Three. Custom string conversions to arrays:

functionStringToArray (str,substr) {vararrtmp=NewArray (); if(substr== "") {Arrtmp.push (str); returnarrtmp; }   vari=0,j=0,k=str.length;  while(i<k) {J=Str.indexof (substr,i); if(J!=-1)     {       if(Str.substring (i,j)! = "") {Arrtmp.push (str.substring (i,j)); } I= J+1; }     Else    {       if(Str.substring (i,k)! = "") {Arrtmp.push (str.substring (i,k)); } I=K; }   }   returnarrtmp;}varStr= "Abc-mng-zhang-mayi"; Console.log (StringToArray (STR,"-") ); Console.log (StringToArray (STR,"-"). length);

The above code also implements the ability to convert a string to an array, following the comments on the code:
Code Comment:
1.function StringToArray (str,substr) {}, this function is used for conversion, STR is the string to be converted, substr is the delimiter.
2. Var arrtmp=new array (), declares an array to hold the segmented string fragment.
3.if (substr== "") {Arrtmp.push (str); return arrtmp;}, if the string delimiter is empty, put the entire string into the array.
4. var i=0,j=0,k=str.length; Declare three variables and assign an initial value, K is the number of characters in the string.
5.while (i<k) {}, a while loop statement, executes the condition that the value of I is less than K, which is less than the number of characters in the string.
6.j=str.indexof (substr,i), used to detect where the delimiter appears in the string, if the IndexOf () function has two arguments, the second argument is to find the beginning of the specified character position, this code should be combined with the following code to understand.
7.if (j!=-1) If the found delimiter exists.
8.if (str.substring (i,j)! = "") {}, intercepts the string from the start find location to the first delimiter found.
9.arrtmp.push (str.substring (I,J)); The truncated string is placed in an array.
10.i=j+1; Sets the position of the start lookup to the next character of the delimiter.
11.else{}, if no lookup is found.
12.if (str.substring (i,k)! = "") {Arrtmp.push (str.substring (I,k));}, if the character after the last delimiter is not empty, add to the array.
13.i=k, set I to K so that the loop stops.
14.return arrtmp; returns an array.
Related reading:
The 1.push () function can refer to the section of the push () method of the JavaScript array object .
The 2.indexOf () function can refer to the indexOf () method section of the JavaScript string object .
The 3.substring () function can refer to the substring () method section of the JavaScript string object .
Four. Convert the custom array to a string:

functionarraytostring (arr,str) {varStrtmp= "";  for(vari=0;i<arr.length;i++)   {     if(arr[i]!= "")     {       if(strtmp== "") {strtmp=Arr[i]; }       Else{strtmp=strtmp+str+Arr[i]; }     }   }   returnstrtmp;}varnewarray=["abc", "Mng", "Zhang", "Mayi"]; Console.log (Arraytostring (NewArray,"-"));

The above code implements our requirements, you can convert the array to a string, the following comments on the code:
Code Comment:
1.function arraytostring (arr,str) {}, the first parameter is an array, and the second parameter is the connection string.
2.var strtmp= "", declares an empty string.
3.for (Var i=0;i<arr.length;i++) {}, iterating through each element in the array.
4.F (arr! = "") {} if the array element is not empty.
5.if (strtmp== "") {Strtmp=arr;}, if the string is also empty, then the element in the array is assigned the value to the string strtmp.
6.else{strtmp=strtmp+str+arr}, otherwise strings are concatenated.
7.return strtmp, returns the converted string.

The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=9405

For more information, refer to: http://www.softwhy.com/javascript/

Mutual conversion of strings and arrays in JavaScript

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.