当前位置:网站首页>js中,字符串和数组互转(一)——字符串转为数组的方法
js中,字符串和数组互转(一)——字符串转为数组的方法
2022-07-06 12:51:00 【viceen】
js中,字符串和数组互转(一)——字符串转为数组的方法
实例
const string = 'uixdk';
// 1. 使用String.prototype.split()方法
string.split('');
// 2. 使用ES6解构运算符
[...string];
// 3. 使用Array.form()
Array.from(string);
// 4. 使用Object.assign()
Object.assign([], string);
//返回结果都是["u", "i", "x", "d", "k"]
方法1、使用split()方法——(推荐)
split()方法用于将字符串拆分为字符串数组,该方法是使用参数中提供的指定分隔符将其分隔为子字符串。
str.split(separator, limit)
参数:
- separator 可选。字符串或正则表达式,从该参数指定的地方分割 string Object。
- limit 可选。该参数可指定返回的数组的最大长度。如果设置了该参数,返回的子串不会多于这个参数指定的数组。如果没有设置该参数,整个字符串都会被分割,不考虑它的长度。
1.1、 一般用法
用一般的字符,例如@或,等符号做分隔符
var str ="北京@北京市@海淀区@学院路";
var splitAdd = str.split("@");
console.log(splitAdd) // ["北京", "北京市", "海淀区", "学院路"]
1.2、需要转义的分隔符
当使用* ^ : | . \
等6个符号做分隔符时,上述6个符号转义字符,必须得加"",即split(“^”)等。
var str1 ="北京*北京市*海淀区*学院路";
var splitAdd1 = str1.split("\*");
console.log(splitAdd1) // ["北京", "北京市", "海淀区", "学院路"]
1.3、是空字符串
1
var str ="北京欢迎你";
var splitAdd3 = str.split("");
console.log(splitAdd3) // ['北', '京', '欢', '迎', '你']
2
var str="Welcome to here";
var n=str.split(" ");
console.log(n); // ['Welcome', 'to', 'here']
方法2、使用ES6扩展运算符
- 语句:
[...string]
var str ="北京欢迎你";
console.log([...str]); // ['北', '京', '欢', '迎', '你']
方法3、使用Array.from()方法——(推荐)
Array.from()方法是javascript中的一个内置函数,它从给定的数组创建一个新的数组实例。
对于字符串,字符串的每个字母表都会转换为新数组实例的元素;
对于整数值,新数组实例simple将获取给定数组的元素。
- 语法:
Array.from(str)
var str ="北京欢迎你";
console.log(Array.from(str)); // ['北', '京', '欢', '迎', '你']
方法4、使用“Object.assign([], string)”语句
var str ="北京欢迎你";
console.log(Object.assign([], str)); // ['北', '京', '欢', '迎', '你']
边栏推荐
- (工作记录)2020年3月11日至2021年3月15日
- 1_ Introduction to go language
- 15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
- Pycharm remote execution
- Pat 1078 hashing (25 points) ⼆ times ⽅ exploration method
- Statistical inference: maximum likelihood estimation, Bayesian estimation and variance deviation decomposition
- Utilisation de l'écran OLED
- Why do novices often fail to answer questions in the programming community, and even get ridiculed?
- OLED屏幕的使用
- OSPF multi zone configuration
猜你喜欢
Reviewer dis's whole research direction is not just reviewing my manuscript. What should I do?
Database - how to get familiar with hundreds of tables of the project -navicat these unique skills, have you got it? (exclusive experience)
【DSP】【第一篇】开始DSP学习
1_ Introduction to go language
2022 refrigeration and air conditioning equipment installation and repair examination contents and new version of refrigeration and air conditioning equipment installation and repair examination quest
New database, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, Feishu multidimensional table, heipayun, Zhixin information, YuQue
3D人脸重建:从基础知识到识别/重建方法!
I've seen many tutorials, but I still can't write a program well. How can I break it?
SAP Fiori应用索引大全工具和 SAP Fiori Tools 的使用介绍
Spark SQL chasing Wife Series (initial understanding)
随机推荐
Activiti global process monitors activitieventlistener to monitor different types of events, which is very convenient without configuring task monitoring in acitivit
【OpenCV 例程200篇】220.对图像进行马赛克处理
1500萬員工輕松管理,雲原生數據庫GaussDB讓HR辦公更高效
Performance test process and plan
Reinforcement learning - learning notes 5 | alphago
[diy] self designed Microsoft makecode arcade, official open source software and hardware
Kubernetes learning summary (20) -- what is the relationship between kubernetes and microservices and containers?
Infrared thermometer based on STM32 single chip microcomputer (with face detection)
LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
PHP online examination system version 4.0 source code computer + mobile terminal
审稿人dis整个研究方向已经不仅仅是在审我的稿子了怎么办?
性能测试过程和计划
Swagger UI教程 API 文档神器
Opencv learning example code 3.2.3 image binarization
Deployment of external server area and dual machine hot standby of firewall Foundation
[200 opencv routines] 220 Mosaic the image
基于STM32单片机设计的红外测温仪(带人脸检测)
Application layer of tcp/ip protocol cluster
Detailed explanation of knowledge map construction process steps
use. Net drives the OLED display of Jetson nano