当前位置:网站首页>In JS, string and array are converted to each other (II) -- the method of converting array into string
In JS, string and array are converted to each other (II) -- the method of converting array into string
2022-07-06 21:10:00 【viceen】
js in , String and array conversion ( Two )—— The method of converting an array into a string
Method 1、toString() Method
1、 function : You can convert each element into a string , Then connect the output with commas and display it .
2、 Usage method :
var arr = [0,1,2,3]; // Define an array
var str = arr.toString(); // hold arr Array utilization toString() Convert to string
console.log(str); // Input string '0,1,2,3'
When the array is in a string environment ,js Automatically called toString() Method to convert an array into a string .
var arr = [0,1,2,3]; // Define an array
var arr1 = [4,5,6,7]; // Define an array
var str = arr + arr1; // Array join operation
console.log(str); // return '0,1,2,34,5,6,7'
var arr = [0,1,2,3]; // Define an array
var arr1 = [4,5,6,7]; // Define an array
var str = arr + ',' + arr1; // Array join operation
console.log(str); // return '0,1,2,3,4,5,6,7'
toString() When converting an array to a string , First, convert each element of the array into a string . When each element is converted to a string , Use commas to separate , Output these strings as a list .
var arr = [[1,[2,3],[4,5]],[6,[7,[8,9],0]]]; // Define multidimensional arrays
var str = arr.toString(); // Convert an array to a string
console.log(str); // Return string '1,2,3,4,5,6,7,8,9,0'
Where array arr It's a multidimensional array ,JavaScript Will call... Iteratively toString() Method converts all arrays to strings .
Method 2、toLocalString() Method
1、 function : And toString() The method usage is basically the same , The difference lies in toLocalString() Method can concatenate the generated string with a user's locale specific delimiter , Form a string .
var array = [1,2,3,4,5]; // Define an array
var str = array.toLocaleString(); // Convert an array to a local string
console.log(str); // Return string '1,2,3,4,5'
- According to Chinese usage habits , First convert the number to a floating-point number, and then perform the string conversion operation
Method 3、join() Method
1、 function : Convert an array to a string , However, it can specify the delimiter , If Omit parameters , By default comma As a separator
var arr = [1,2,3]; // Define an array
var str = arr.join("-"); // Specify the separator -
console.log(str); // Return string '1-2-3'
var arr = [1,2,3]; // Define an array
var str = arr.join(","); // Specify the separator
console.log(str); // Return string '1,2,3'
边栏推荐
- Ravendb starts -- document metadata
- Variable star --- article module (1)
- 基于STM32单片机设计的红外测温仪(带人脸检测)
- Laravel notes - add the function of locking accounts after 5 login failures in user-defined login (improve system security)
- Kubernetes learning summary (20) -- what is the relationship between kubernetes and microservices and containers?
- 防火墙基础之外网服务器区部署和双机热备
- 15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
- js之遍历数组、字符串
- 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
- Pat 1078 hashing (25 points) ⼆ times ⽅ exploration method
猜你喜欢
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
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
Reinforcement learning - learning notes 5 | alphago
性能测试过程和计划
Infrared thermometer based on STM32 single chip microcomputer (with face detection)
基于深度学习的参考帧生成
数据湖(八):Iceberg数据存储格式
PHP saves session data to MySQL database
【OpenCV 例程200篇】220.对图像进行马赛克处理
随机推荐
快过年了,心也懒了
Can novices speculate in stocks for 200 yuan? Is the securities account given by qiniu safe?
'class file has wrong version 52.0, should be 50.0' - class file has wrong version 52.0, should be 50.0
OSPF multi zone configuration
SAP UI5 框架的 manifest.json
js中,字符串和数组互转(一)——字符串转为数组的方法
如何实现常见框架
防火墙基础之外网服务器区部署和双机热备
PG basics -- Logical Structure Management (transaction)
The biggest pain point of traffic management - the resource utilization rate cannot go up
HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
Variable star --- article module (1)
Nodejs教程之Expressjs一篇文章快速入门
Interviewer: what is the internal implementation of ordered collection in redis?
ICML 2022 | Flowformer: 任务通用的线性复杂度Transformer
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
@GetMapping、@PostMapping 和 @RequestMapping详细区别附实战代码(全)
968 edit distance
拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条
New database, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, Feishu multidimensional table, heipayun, Zhixin information, YuQue