JS将字符串拆分为字符数组

2021-06-10  本文已影响0人  祈澈菇凉

有多种方法可以将字符串分割成字符数组,我更喜欢使用扩展操作符(...):

<!DOCTYPE HTML>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
        <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
        <script src="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
        <style>
        </style>
    </head>
    <body>
    </body>
    <script type="text/javascript">
        const a = 'wangting'
        const characters = [...a]
        console.log(characters) // ["w", "a", "n", "g", "t", "i", "n", "g"]
    </script>
</html>

上一篇下一篇

猜你喜欢

热点阅读