使用JavaScript将字符串转换为字符数组

在JavaScript中,可以使用split()和Array.from()方法将字符串转换为字符串数组。下面本篇文章就来给大家介绍一下,希望对大家有所帮助。

方法1:使用split()方法

split()方法用于将给定字符串拆分为字符串数组,该方法是使用参数中提供的指定分隔符将其分隔为子字符串。

语法:

str.split(separator, limit)

示例:

Welcome to here!

输出:

Welcome to here!

W,e,l,c,o,m,e, ,t,o, ,h,e,r,e,!

方法2:使用Array.from()方法

Array.from()方法是javascript中的一个内置函数,它从给定的数组创建一个新的数组实例。对于字符串,字符串的每个字母表都会转换为新数组实例的元素;对于整数值,新数组实例simple将获取给定数组的元素。

语法:

Array.from(str)

示例:

Welcome to here!

输出:

Welcome to here!

W,e,l,c,o,m,e, ,t,o, ,h,e,r,e,!

更多前端开发知识,请查阅 HTML中文网 !!

你可能感兴趣的:(使用JavaScript将字符串转换为字符数组)