简体   繁体   中英

In javascript how to convert string to array and array to string

在JavaScript和Jquery中如何将字符串转换为数组,并将相同的数组转换为字符串,并使用JavaScript中的typeof方法检查它们。

var arr = "abcdef".split(''); // creates an array from a string

var str = arr.join(''); // creates a string from that above array

From String to Array you can Use split() Method

var str = "How are you doing today?";
var res = str.split(" ");

console.log(res); // How,are,you,doing,today? it will print

From Array to String you can use Join() method or toString() Method

If you want do it manually, without any JavaScript methods. Try the below

String to Array

var str = "STRING";
var arr = [];
for(int i=0; i<=str.length; i++)
    arr[i] = str.charAt(i);

Array to String

var str = "";
var arr = ["S","T","R","I","G"];
for(int i=0; i<=arr.length; i++)
    str +=arr.charAt(i);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM