当前位置:网站首页>In JS, string and array are converted to each other (I) -- the method of converting string into array
In JS, string and array are converted to each other (I) -- the method of converting string into array
2022-07-06 21:11:00 【viceen】
js in , String and array conversion ( One )—— The method of converting a string into an array
example
const string = 'uixdk';
// 1. Use String.prototype.split() Method
string.split('');
// 2. Use ES6 Deconstruction operator
[...string];
// 3. Use Array.form()
Array.from(string);
// 4. Use Object.assign()
Object.assign([], string);
// The results are all ["u", "i", "x", "d", "k"]
Method 1、 Use split() Method ——( recommend )
split() Method is used to split a string into an array of strings , This method uses the specified separator provided in the parameter to separate it into substrings .
str.split(separator, limit)
Parameters :
- separator Optional . String or regular expression , Split from where specified by this parameter string Object.
- limit Optional . This parameter specifies the maximum length of the returned array . If this parameter is set , No more substrings will be returned than the array specified by this parameter . If the parameter is not set , The entire string will be split , Regardless of its length .
1.1、 General usage
Use common characters , for example @ or , Etc. as separator
var str =" Beijing @ The Beijing municipal @ Haidian District @ Xueyuan Road ";
var splitAdd = str.split("@");
console.log(splitAdd) // [" Beijing ", " The Beijing municipal ", " Haidian District ", " Xueyuan Road "]
1.2、 Separator that needs escape
When using * ^ : | . \ etc. 6 When a symbol is used as a separator , Above 6 Symbol escape characters , Must add "", namely split(“^”) etc. .
var str1 =" Beijing * The Beijing municipal * Haidian District * Xueyuan Road ";
var splitAdd1 = str1.split("\*");
console.log(splitAdd1) // [" Beijing ", " The Beijing municipal ", " Haidian District ", " Xueyuan Road "]
1.3、 Is an empty string
1
var str =" welcome to beijing ";
var splitAdd3 = str.split("");
console.log(splitAdd3) // [' north ', ' Beijing ', ' huan ', ' To meet ', ' you ']
2
var str="Welcome to here";
var n=str.split(" ");
console.log(n); // ['Welcome', 'to', 'here']
Method 2、 Use ES6 Extension operator
- sentence :
[...string]
var str =" welcome to beijing ";
console.log([...str]); // [' north ', ' Beijing ', ' huan ', ' To meet ', ' you ']
Method 3、 Use Array.from() Method ——( recommend )
Array.from() The method is javascript A built-in function in , It creates a new array instance from a given array .
For strings , Each alphabet of the string is converted to the elements of the new array instance ;
For integer values , New array instance simple Will get the elements of the given array .
- grammar :
Array.from(str)
var str =" welcome to beijing ";
console.log(Array.from(str)); // [' north ', ' Beijing ', ' huan ', ' To meet ', ' you ']
Method 4、 Use “Object.assign([], string)” sentence
var str =" welcome to beijing ";
console.log(Object.assign([], str)); // [' north ', ' Beijing ', ' huan ', ' To meet ', ' you ']
边栏推荐
- Nodejs tutorial let's create your first expressjs application with typescript
- R语言做文本挖掘 Part4文本分类
- @PathVariable
- 愛可可AI前沿推介(7.6)
- 面试官:Redis中有序集合的内部实现方式是什么?
- PHP saves session data to MySQL database
- Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
- 每个程序员必须掌握的常用英语词汇(建议收藏)
- Mtcnn face detection
- OSPF multi zone configuration
猜你喜欢

The biggest pain point of traffic management - the resource utilization rate cannot go up

Reinforcement learning - learning notes 5 | alphago

Study notes of grain Mall - phase I: Project Introduction

Why do job hopping take more than promotion?

Swagger UI tutorial API document artifact

愛可可AI前沿推介(7.6)

OneNote 深度评测:使用资源、插件、模版

for循环中break与continue的区别——break-完全结束循环 & continue-终止本次循环

KDD 2022 | 通过知识增强的提示学习实现统一的对话式推荐

966 minimum path sum
随机推荐
Thinking about agile development
Regular expression collection
【mysql】触发器
Web开发小妙招:巧用ThreadLocal规避层层传值
Infrared thermometer based on STM32 single chip microcomputer (with face detection)
SAP UI5 框架的 manifest.json
性能测试过程和计划
【滑动窗口】第九届蓝桥杯省赛B组:日志统计
Data Lake (VIII): Iceberg data storage format
拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条
Is it safe to open an account in flush? Which securities company is good at opening an account? Low handling charges
OSPF多区域配置
ICML 2022 | Flowformer: 任务通用的线性复杂度Transformer
Aiko ai Frontier promotion (7.6)
This year, Jianzhi Tencent
面试官:Redis中有序集合的内部实现方式是什么?
PG基础篇--逻辑结构管理(事务)
【OpenCV 例程200篇】220.对图像进行马赛克处理
新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
OneNote in-depth evaluation: using resources, plug-ins, templates