当前位置:网站首页>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 ']
边栏推荐
- El table table - get the row and column you click & the sort of El table and sort change, El table column and sort method & clear sort clearsort
- KDD 2022 | realize unified conversational recommendation through knowledge enhanced prompt learning
- 面试官:Redis中有序集合的内部实现方式是什么?
- Yyds dry goods count re comb this of arrow function
- 硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
- 1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
- Distributed ID
- 防火墙基础之外网服务器区部署和双机热备
- 爱可可AI前沿推介(7.6)
- Mtcnn face detection
猜你喜欢
Infrared thermometer based on STM32 single chip microcomputer (with face detection)
Spark SQL chasing Wife Series (initial understanding)
性能测试过程和计划
Pycharm remote execution
OAI 5G NR+USRP B210安装搭建
【mysql】触发器
基于深度学习的参考帧生成
1_ Introduction to go language
Variable star --- article module (1)
每个程序员必须掌握的常用英语词汇(建议收藏)
随机推荐
What is the difference between procedural SQL and C language in defining variables
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
Reference frame generation based on deep learning
Opencv learning example code 3.2.3 image binarization
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
c#使用oracle存储过程获取结果集实例
字符串的使用方法之startwith()-以XX开头、endsWith()-以XX结尾、trim()-删除两端空格
OneNote in-depth evaluation: using resources, plug-ins, templates
Manifest of SAP ui5 framework json
Seven original sins of embedded development
[asp.net core] set the format of Web API response data -- formatfilter feature
20220211 failure - maximum amount of data supported by mongodb
监控界的最强王者,没有之一!
OSPF multi zone configuration
What's the best way to get TFS to output each project to its own directory?
Notes - detailed steps of training, testing and verification of yolo-v4-tiny source code
Why do job hopping take more than promotion?
如何实现常见框架
【论文解读】用于白内障分级/分类的机器学习技术
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件