当前位置:网站首页>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)); // ['北', '京', '欢', '迎', '你']
边栏推荐
- I've seen many tutorials, but I still can't write a program well. How can I break it?
- Le langage r visualise les relations entre plus de deux variables de classification (catégories), crée des plots Mosaiques en utilisant la fonction Mosaic dans le paquet VCD, et visualise les relation
- Spiral square PTA
- 使用.Net驱动Jetson Nano的OLED显示屏
- [weekly pit] information encryption + [answer] positive integer factorization prime factor
- 【微信小程序】運行機制和更新機制
- Laravel笔记-自定义登录中新增登录5次失败锁账户功能(提高系统安全性)
- PG基础篇--逻辑结构管理(事务)
- LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
- 请问sql group by 语句问题
猜你喜欢

No Yum source to install SPuG monitoring

Deployment of external server area and dual machine hot standby of firewall Foundation
![[diy] how to make a personalized radio](/img/fc/a371322258131d1dc617ce18490baf.jpg)
[diy] how to make a personalized radio

Use of OLED screen

每个程序员必须掌握的常用英语词汇(建议收藏)

硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件

What is the problem with the SQL group by statement

Why do novices often fail to answer questions in the programming community, and even get ridiculed?

APS taps home appliance industry into new growth points

使用.Net分析.Net达人挑战赛参与情况
随机推荐
OAI 5g nr+usrp b210 installation and construction
OLED屏幕的使用
What programming do children learn?
基于深度学习的参考帧生成
Core principles of video games
R language visualizes the relationship between more than two classification (category) variables, uses mosaic function in VCD package to create mosaic plots, and visualizes the relationship between tw
"Penalty kick" games
SAP Fiori应用索引大全工具和 SAP Fiori Tools 的使用介绍
Manifest of SAP ui5 framework json
Review questions of anatomy and physiology · VIII blood system
Data Lake (VIII): Iceberg data storage format
15 millions d'employés sont faciles à gérer et la base de données native du cloud gaussdb rend le Bureau des RH plus efficace
[DIY]自己设计微软MakeCode街机,官方开源软硬件
Spiral square PTA
Quel genre de programmation les enfants apprennent - ils?
7. Data permission annotation
Pat 1078 hashing (25 points) ⼆ times ⽅ exploration method
Common doubts about the introduction of APS by enterprises
##无yum源安装spug监控
Summary of different configurations of PHP Xdebug 3 and xdebug2