zoukankan      html  css  js  c++  java
  • javascript中的字符串和数组的互转

    javascript中的数组转换成字符串用toString(),或者用join()。

    将数组和字符串用+连接,赋给一个变量,这个变量自动转变成字符串了。

    字符串转换成数组用split(',')

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>js中数组和字符串的互转</title>
    </head>
    
    <body>
    
        <script>
    
            var msgsnew = ["test", "test", "test", "test", "test"];
            //var tstr=msgsnew.join()        
            var tstr = msgsnew.join(',');    //数组转字符串
            console.log("tstr", tstr);
            var tstrtwo = msgsnew.toString();       //数组转字符串
            var tstrthree = "可以转字符串的" + msgsnew    //js中的+
            console.log("+后转变成字符串的", tstrthree, typeof tstrthree);
            console.log("数组:" + msgsnew + ":" + (msgsnew instanceof Array), "数组转字符串:" + tstrtwo + ":" + (tstrtwo instanceof Array));
            console.log(msgsnew);
    
            var tarra = tstr.split(',');    //字符串转数组
    
            console.log(msgsnew instanceof Array, typeof tstr, tarra);
        </script>
    
    </body>
    
    </html>
    sometimes the hardest part isn't letting go,but rather start over
  • 相关阅读:
    LInux设备驱动分析—— kmalloc和kzalloc函数
    g++使用总结
    Redis那些事(一) — Redis简介
    C++后台知识点总结(一)
    秋招复习-C++(三)
    C++之类成员的访问权限详解(一)
    c++内联函数解析(inline)
    论文图片
    markdown Typora学习随笔
    ROS 导航设计随笔
  • 原文地址:https://www.cnblogs.com/zhumeiming/p/9803772.html
Copyright © 2011-2022 走看看