当前位置:网站首页>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)); // ['北', '京', '欢', '迎', '你']
边栏推荐
- Summary of different configurations of PHP Xdebug 3 and xdebug2
- Infrared thermometer based on STM32 single chip microcomputer (with face detection)
- Laravel笔记-自定义登录中新增登录5次失败锁账户功能(提高系统安全性)
- Notes - detailed steps of training, testing and verification of yolo-v4-tiny source code
- Design your security architecture OKR
- It's almost the new year, and my heart is lazy
- @PathVariable
- 【微信小程序】運行機制和更新機制
- [MySQL] basic use of cursor
- 知识图谱构建流程步骤详解
猜你喜欢
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
Use of OLED screen
use. Net drives the OLED display of Jetson nano
[DIY]如何制作一款個性的收音機
Swagger UI教程 API 文档神器
Opencv learning example code 3.2.3 image binarization
【mysql】触发器
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
小孩子學什麼編程?
[diy] self designed Microsoft makecode arcade, official open source software and hardware
随机推荐
Is it safe to open an account in flush? Which securities company is good at opening an account? Low handling charges
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
What is the difference between procedural SQL and C language in defining variables
SAP UI5 框架的 manifest.json
OSPF多区域配置
华为设备命令
过程化sql在定义变量上与c语言中的变量定义有什么区别
C language games - minesweeping
Summary of different configurations of PHP Xdebug 3 and xdebug2
Why do novices often fail to answer questions in the programming community, and even get ridiculed?
Notes - detailed steps of training, testing and verification of yolo-v4-tiny source code
Interviewer: what is the internal implementation of ordered collection in redis?
Mécanisme de fonctionnement et de mise à jour de [Widget Wechat]
每个程序员必须掌握的常用英语词汇(建议收藏)
No Yum source to install SPuG monitoring
use. Net analysis Net talent challenge participation
What key progress has been made in deep learning in 2021?
Recyclerview GridLayout bisects the middle blank area
【DSP】【第二篇】了解C6678和创建工程
Use of OLED screen