一、数组转字符串
需要将数组元素用某个字符连接成字符串,示例代码如下
<!doctype html> <html> <head> <meta charset="utf-8"> <title>数组转化为字符串</title> </head> <body> <script type="text/javascript"> var a, b,c; a = new Array(11,22,33,44,55); b = a.join('-'); c = a.join(''); document.writeln(a.constructor);//输出:function Array() { [native code] } document.writeln(b);//11-22-33-44-55 使用-拼接数组元素 document.writeln(c);//1122334455 </script> </body> </html>
二、字符串转化为数组:
实现方法为将字符串按某个字符切割成若干个字符串,并以数组形式返回,示例代码如下:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>字符串转化为数组</title> </head> <body> <script type="text/javascript"> var str = 'ab+c+de'; var a = str.split('+'); var b = str.split(''); document.writeln(a);// [ab, c, de] document.writeln(b);//[a, b, +, c, +, d, e] </script> </body> </html>