当前位置:网站首页>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'
边栏推荐
- 【滑动窗口】第九届蓝桥杯省赛B组:日志统计
- OAI 5G NR+USRP B210安装搭建
- 1_ Introduction to go language
- Reviewer dis's whole research direction is not just reviewing my manuscript. What should I do?
- Reference frame generation based on deep learning
- 【深度学习】PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
- Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
- Aiko ai Frontier promotion (7.6)
- SAP Fiori应用索引大全工具和 SAP Fiori Tools 的使用介绍
- What's the best way to get TFS to output each project to its own directory?
猜你喜欢

OneNote 深度评测:使用资源、插件、模版

请问sql group by 语句问题

Laravel notes - add the function of locking accounts after 5 login failures in user-defined login (improve system security)

KDD 2022 | realize unified conversational recommendation through knowledge enhanced prompt learning

Aike AI frontier promotion (7.6)

Study notes of grain Mall - phase I: Project Introduction

Seven original sins of embedded development

新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀

##无yum源安装spug监控

MLP (multilayer perceptron neural network) is a multilayer fully connected neural network model.
随机推荐
@PathVariable
Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
Laravel笔记-自定义登录中新增登录5次失败锁账户功能(提高系统安全性)
What key progress has been made in deep learning in 2021?
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
OneNote 深度评测:使用资源、插件、模版
Kubernetes learning summary (20) -- what is the relationship between kubernetes and microservices and containers?
快过年了,心也懒了
Seven original sins of embedded development
【OpenCV 例程200篇】220.对图像进行马赛克处理
【Redis设计与实现】第一部分 :Redis数据结构和对象 总结
@GetMapping、@PostMapping 和 @RequestMapping详细区别附实战代码(全)
代理和反向代理
1_ Introduction to go language
Select data Column subset in table R [duplicate] - select subset of columns in data table R [duplicate]
面试官:Redis中有序集合的内部实现方式是什么?
Yyds dry goods count re comb this of arrow function
It's almost the new year, and my heart is lazy
Is it safe to open an account in flush? Which securities company is good at opening an account? Low handling charges
字符串的使用方法之startwith()-以XX开头、endsWith()-以XX结尾、trim()-删除两端空格