javascript 在javascript中如何将字符串转换为数组并将数组转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32067763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In javascript how to convert string to array and array to string
提问by Raghul Rajendran
In JavaScript and Jquery how to convert the string to array and same array convert to string and check them using typeof method in JavaScript.
在 JavaScript 和 Jquery 如何将字符串转换为数组和相同的数组转换为字符串并使用 JavaScript 中的 typeof 方法检查它们。
回答by Jaromanda X
var arr = "abcdef".split(''); // creates an array from a string
var str = arr.join(''); // creates a string from that above array
回答by stackover flow
From String to Array you can Use split()
Method
从字符串到数组,您可以使用split()
方法
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
从数组到字符串,您可以使用Join()
方法或toString()
方法
回答by RevanthKrishnaKumar V.
If you want do it manually, without any JavaScript methods. Try the below
如果你想手动完成,没有任何 JavaScript 方法。试试下面的
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);