js implements conversion operations between strings and arrays

  • 2020-11-30 08:10:43
  • OfStack

This article introduces the javascript string and array conversion method, share for your reference, the specific content is as follows
String and an array of mutual conversion operations is very important, because often can be used in the actual process of coding, so it is a must to grasp knowledge, of course, this knowledge is not difficult, know will never know, need a lot of practice to master is not the sort of thing, do the following 1 under the simple introduction.
1. Convert strings to arrays
This is done using the split() function, which converts a string into an array with the specified character as a delimiter, as follows:


var Str="abc-mng-zhang-mayi";
var newArray=Str.split("-");
console.log(newArray); 

As you can see from the output, the split() function has converted the string to an array.
2. Convert an array to a string
This can be done using the join() function of the Array object, which concatenates the elements of an 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 does what we want, concatenating the array elements with "-" and generating a string.
Both of the above examples use native functions, but we can also write our own for greater flexibility and knowledge.
3. Custom string conversion to array


function StringToArray(str,substr) 
{ 
 var arrTmp=new Array(); 
 if(substr=="") 
 { 
 arrTmp.push(str); 
 return arrTmp; 
 } 
 var i=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; 
 } 
 } 
 return arrTmp; 
}
var Str="abc-mng-zhang-mayi";
console.log(StringToArray(Str,"-"));
console.log(StringToArray(Str,"-").length);

The above code also realizes the function of converting strings into arrays. The following code is commented as follows:
Code comments:
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(), declare 1 array to hold the split string fragment.
3.if(substr=="") {arrTmp.push(str); return arrTmp; }, if the string delimiter is empty, the entire string is put into the array.
4. var i=0,j=0,k=str.length; Declare three variables and assign an initial value. The value of k is the number of characters in the string.
5.while(i < k){}, 1 while loop, executed if 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 the position of the separator in the string. If the indexOf() function takes two arguments, the second argument is to find the starting position of the specified character.
7.if(j! =-1), if the lookup separator exists.
8.if(str.substring(i,j)! =""){}, intercepting the string from the starting lookup position to the first delimiter.
9.arrTmp.push(str.substring(i,j)); To place the truncated string into an array.
10.i=j+1; Sets the location at which the lookup begins to the next character of the separator.
11.else{}, if not found.
12.if(str.substring(i,k)! =""){arrTmp.push(str.substring(i,k)); }, if the character after the last 1 separator is not null, then it is added to the array.
13.i=k, set i to k so that the loop stops.
14.return arrTmp; Returns an array.
Related knowledge:
1. Definition and usage of push() method:
This method appends one or more new elements to the end of the specified array and returns the length of the array.
Note: The new element is appended directly to the original array rather than creating a new array.
Click to see arrays for more properties and methods.
Grammatical structure:
arrayObject.push(elements 1, 2... , the element N)

Parameter list:
Parameters to describe
Parameters (1... N) required. The new element to be appended.

Example code:


var a = [1,2,3];
console.log(a.push("zhang","dao"));

2. Definition and usage of indexOf() :
This method returns the location of the specified string when it first appears in the string.
If no corresponding string is retrieved, the return value is -1.
Note: This method is case sensitive.
Grammatical structure:
stringObject.indexOf(substring,startindex)

Example code:


var a=new String("abcdefg")
console.log(a.indexOf("b"));

b appears second in the string abcdefg. Output :1.


var a=new String("abcdefg")
console.log(a.indexOf("B"));

This method is case-sensitive, so there is no uppercase B in the string abcdefg. Output :-1.


var a=new String("abcdefg")
console.log(a.indexOf("e",4));

The retrieval starts at position 4, and the first occurrence of the string is evaluated from the beginning of the string. Output :4.

3. substring () function.

4. Convert custom arrays to strings


function ArrayToString(arr,str) 
{ 
 var strTmp=""; 
 for(var i=0;i<arr.length;i++) 
 { 
 if(arr[i]!="") 
 { 
  if(strTmp=="") 
  { 
  strTmp = arr[i]; 
  } 
  else
  { 
  strTmp=strTmp+str+arr[i]; 
  } 
 } 
 } 
 return strTmp; 
}
var newArray=["abc","mng","zhang","mayi"]; 
console.log(ArrayToString(newArray,"-"));

The above code has realized our requirements. We can convert an array into a string. The following code is commented as follows:
Code comments:
function ArrayToString(arr,str){}, the first parameter is an array, the second parameter is a connection string.
var strTmp="", declares 1 empty string.
3.for(var i=0;i < arr.length; i++) {}, traversing each element in the number group.
4.f(arr! =""){} if the array element is not empty.
5.if(strTmp=="") {strTmp=arr; }, assign this element in the array to the string strTmp if the string is also empty.
6.else{strTmp=strTmp+str+arr}, otherwise string concatenation.
7.return strTmp, returns the converted string.

Above is the js string and array to achieve the conversion between the detailed code, I hope to help you learn.


Related articles: