Home  >  Article  >  Web Front-end  >  Summary of javascript string and array conversion_javascript skills

Summary of javascript string and array conversion_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:57:461416browse

数组转字符串

1.join()方法

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


字符串转数组

1.call()方法

var str = "adafgfsg";
var strArr = Array.prototype.slice.call(str,0);
console.log(strArr);

结果:["a", "d", "a", "f", "g", "f", "s", "g"]

2.正则表达式

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);

结果:["a", "d", "a", "f", "g", "f", "s", "g"]

3.直接用split()方法

var str = "adafgfsg";
var strArr = str.split('');
console.log(strArr);

结果:["a", "d", "a", "f", "g", "f", "s", "g"]

以上所述就是本文都给大家总结的全部内容了,希望大家能够喜欢。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn