This article will share with you the simple and practical functions of converting strings into arrays and arrays into strings in Js. For more information, see.
Convert array to string
1. join () method
var s= ["a", "d", "a", "f", "g", "f", "s", "g"];s.join() //"a,d,a,f,g,f,s,g"s.join(" ") //"a d a f g f s g"s.join(''); //adafgfsg
String to array
1. call () method
var str = "adafgfsg";var strArr = Array.prototype.slice.call(str,0);console.log(strArr);
Result: ["a", "d", "a", "f", "g", "f", "s", "g"]
2. Regular Expression
var str = "adafgfsg";var strArr = str.replace(/(.)(?=.)/g,'$1,').split(',');console.log(strArr);var str = "adafgfsg";var strArr = str.match(/\w/g);console.log(strArr);
Result: ["a", "d", "a", "f", "g", "f", "s", "g"]
3. directly use the split () method
var str = "adafgfsg";var strArr = str.split('');console.log(strArr);
Result: ["a", "d", "a", "f", "g", "f", "s", "g"]
The above is all the content summarized in this article. I hope you will like it.