js数组与字符串的相互转换方法
1、数组转字符串
需要将数组元素用某个字符连接成字符串,示例代码如下
1 var a, b; 2 a = new Array(0,1,2,3,4); 3 b = a.join("-"); //"0-1-2-3-4"
2、字符串转数组
实现方法为将字符串按某个字符切割成若干个字符串,并以数组形式返回,示例代码如下:
1 var s = "abc,abcd,aaa"; 2 ss = s.split(",");// 在每个逗号(,)处进行分解 ["abc", "abcd", "aaa"] 3 4 var s1 = "helloworld"; 5 ss1 = s1.split(''); //["h", "e", "l", "l", "o", "w", "o", "r", "l", "d"]
[Haima的博客]
http://www.cnblogs.com/haima/