当前位置:网站首页>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 ']
边栏推荐
- 3D人脸重建:从基础知识到识别/重建方法!
- 嵌入式开发的7大原罪
- [MySQL] trigger
- 字符串的使用方法之startwith()-以XX开头、endsWith()-以XX结尾、trim()-删除两端空格
- 1500萬員工輕松管理,雲原生數據庫GaussDB讓HR辦公更高效
- 20220211 failure - maximum amount of data supported by mongodb
- 分糖果
- 039. (2.8) thoughts in the ward
- 基于深度学习的参考帧生成
- Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
猜你喜欢
Distributed ID
Laravel notes - add the function of locking accounts after 5 login failures in user-defined login (improve system security)
[MySQL] basic use of cursor
3D人脸重建:从基础知识到识别/重建方法!
20220211 failure - maximum amount of data supported by mongodb
爱可可AI前沿推介(7.6)
The most comprehensive new database in the whole network, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, flying Book Multidimensional table, heipayun, Zhix
SAP UI5 框架的 manifest.json
基于STM32单片机设计的红外测温仪(带人脸检测)
Variable star --- article module (1)
随机推荐
What is the difference between procedural SQL and C language in defining variables
OSPF multi zone configuration
Dynamically switch data sources
愛可可AI前沿推介(7.6)
Common English vocabulary that every programmer must master (recommended Collection)
SAP UI5 框架的 manifest.json
Simple continuous viewing PTA
for循环中break与continue的区别——break-完全结束循环 & continue-终止本次循环
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
Is it safe to open an account in flush? Which securities company is good at opening an account? Low handling charges
Nodejs tutorial expressjs article quick start
什么是RDB和AOF
Nodejs教程之Expressjs一篇文章快速入门
Is this the feeling of being spoiled by bytes?
JS操作dom元素(一)——获取DOM节点的六种方式
[asp.net core] set the format of Web API response data -- formatfilter feature
js之遍历数组、字符串
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
SAP Fiori应用索引大全工具和 SAP Fiori Tools 的使用介绍
@PathVariable