当前位置:网站首页>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 ']
边栏推荐
- Interviewer: what is the internal implementation of ordered collection in redis?
- Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
- Yyds dry goods count re comb this of arrow function
- 1_ Introduction to go language
- Nodejs tutorial expressjs article quick start
- 爱可可AI前沿推介(7.6)
- LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
- 967- letter combination of telephone number
- What is the problem with the SQL group by statement
- @GetMapping、@PostMapping 和 @RequestMapping详细区别附实战代码(全)
猜你喜欢
愛可可AI前沿推介(7.6)
1500萬員工輕松管理,雲原生數據庫GaussDB讓HR辦公更高效
After working for 5 years, this experience is left when you reach P7. You have helped your friends get 10 offers
Data Lake (VIII): Iceberg data storage format
LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
Is this the feeling of being spoiled by bytes?
2022 fields Award Announced! The first Korean Xu Long'er was on the list, and four post-80s women won the prize. Ukrainian female mathematicians became the only two women to win the prize in history
Aike AI frontier promotion (7.6)
Redis insert data garbled solution
每个程序员必须掌握的常用英语词汇(建议收藏)
随机推荐
[wechat applet] operation mechanism and update mechanism
LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
Thinking about agile development
Mtcnn face detection
Dynamically switch data sources
Interviewer: what is the internal implementation of ordered collection in redis?
OAI 5g nr+usrp b210 installation and construction
3D人脸重建:从基础知识到识别/重建方法!
R语言可视化两个以上的分类(类别)变量之间的关系、使用vcd包中的Mosaic函数创建马赛克图( Mosaic plots)、分别可视化两个、三个、四个分类变量的关系的马赛克图
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
ACdreamoj1110(多重背包)
HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
Vim 基本配置和经常使用的命令
Pinduoduo lost the lawsuit, and the case of bargain price difference of 0.9% was sentenced; Wechat internal test, the same mobile phone number can register two account functions; 2022 fields Awards an
【滑动窗口】第九届蓝桥杯省赛B组:日志统计
Reference frame generation based on deep learning
Regular expression collection
正则表达式收集
Pat 1078 hashing (25 points) ⼆ times ⽅ exploration method
js中,字符串和数组互转(一)——字符串转为数组的方法