Home > Web Front-end > JS Tutorial > body text

Summary of javascript string and array conversion_javascript skills

WBOY
Release: 2016-05-16 15:57:46
Original
1376 people have browsed it

数组转字符串

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


Copy after login

字符串转数组

1.call()方法

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

结果:["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);
Copy after login

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

3.直接用split()方法

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

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

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!