Js converts strings and arrays, and js converts each other.
This document describes how to convert strings and arrays in javascript. The following describes how to convert strings and arrays.
Conversion between strings and arrays is very important, because it is often used in the actual encoding process, so this is a required knowledge point, of course, this knowledge point is not difficult, once you know it, you will always know that it is not something that requires full practice. Let's give a brief introduction.
1. String Conversion to array
This operation uses the split () function, which can use the specified character as the separator and convert the string into an array. The instance code is as follows:
var Str="abc-mng-zhang-mayi";var newArray=Str.split("-");console.log(newArray);
The output result shows that the split () function has converted the string into an array.
2. Convert the array to a string
This operation can be implemented using the join () function of the Array object. This function can connect elements in the Array with specified characters and then return the generated string.
The Code is as follows:
var newArray=["abc","mng","zhang","mayi"];var Str=newArray.join("-");console.log(Str);
The above Code meets our requirements. We use "-" to connect the array elements and generate a string.
The above two examples use built-in functions. Of course, we can also write them by ourselves, which is more flexible and knowledgeable.
3. convert a custom string to an 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 code above also implements the function of converting a string to an array. Next, let's comment on the Code:
Code comment:
1. function StringToArray (str, substr) {}. This function is used for conversion. str is the string to be converted, and substr is the separator.
2. var arrTmp = new Array (), which declares an Array to store split string fragments.
3. if (substr = "") {arrTmp. push (str); return arrTmp;}. if the string separator is empty, the entire string is placed in an array.
4. var I = 0, j = 0, k = str. length; declare three variables and assign the initial values. The value of k is the number of characters in the string.
5. while (I <k) {} is a while loop statement that is executed on the condition that the value of I is smaller than k, that is, the number of characters in the string.
6. j = str. indexOf (substr, I) is used to detect the position of the separator in the string. If the indexOf () function has two parameters, the second parameter is used to find the starting position of the specified character, this code should be understood in conjunction with the following code.
7. if (j! =-1), if the separator to be searched exists.
8. if (str. substring (I, j )! = "") {}, Truncates the string from the start position to the first separator.
9. arrTmp. push (str. substring (I, j);: place the intercepted string into the array.
10. I = j + 1; set the start position 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 separator is not empty, add it 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 the push () method:
This method can append one or more new elements to the end of the specified array and return the length of the array.
Note: The new element is directly appended to the original array, rather than creating a new array.
Click here to see more attributes and methods of arrays.
Syntax structure:
ArrayObject. push (element 1, element 2,..., element N)
Parameter List:
Parameter description
The parameter (1... N) is required. The new element to be appended.
Instance code:
var a = [1,2,3];console.log(a.push("zhang","dao"));
2. Definition and usage of the indexOf () method:
This method returns the position of the specified string that appears for the first time in the string.
If no corresponding string is retrieved, the returned value is-1.
Note: This method is case sensitive.
Syntax structure:
StringObject. indexOf (substring, startindex)
Instance code:
var a=new String("abcdefg")console.log(a.indexOf("b"));
B appears in the second abcdefg string. Output result: 1.
var a=new String("abcdefg")console.log(a.indexOf("B"));
This method is case sensitive, so the string abcdefg does not contain uppercase B. Output result:-1.
var a=new String("abcdefg")console.log(a.indexOf("e",4));
The Search Start position is 4, and the first occurrence position of the string is calculated from the start of the string. Output result: 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 meets our requirements. We can convert the array into a string and comment on the code below:
Code comment:
1. function ArrayToString (arr, str) {}. The first parameter is an array, and the second parameter is a connection string.
2. var strTmp = "", declare an empty string.
3. for (var I = 0; I <arr. length; I ++) {}, traverse 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, assign this element to the strTmp string.
6. else {strTmp = strTmp + str + arr}; otherwise, the string is connected.
7. return strTmp, return the converted string.
The above is the detailed code for converting strings and arrays in js. I hope it will be helpful for you to learn.
Articles you may be interested in:
- Javascript Splits a string into an array based on specified characters
- JavaScript string and array conversion functions [do not use split and join]
- Js split usage and definition js split string into array instance code
- How to parse json data in JS and convert json strings into Arrays
- How to convert a string into an object or array sample code in js
- Example of splitting strings into small arrays using the split function in js
- JavaScript array (array) uses a string as the array's underlying method
- Js determines the data type, such as determining whether the array is a string or not.
- Mutual conversion between js arrays and strings
- Discuss the performance of js String Array concatenation