zoukankan      html  css  js  c++  java
  • 内置对象的相互转化

    一.Number转化为String: 

    1.toString()

    var age = 11;
    var ageString = age.toString();
    console.log(typeof age);//Number
    console.log(typeof ageString);//String

    2.obj + ""

    var age = 11;
    var ageString = age + "";
    console.log(typeof age);//Number
    console.log(typeof ageString);//String

    3.String()

    var age = 11;
    var ageString = String(age);
    console.log(typeof age);//Number
    console.log(typeof ageString);//String

    二.String转化为Number

    1.parseFloat()或parseInt()

    var age = "11";
    var ageNumber = parseInt(age);
    console.log(typeof age);//String
    console.log(typeof ageNumber);//Number

    2.obj*1

    var age = "11";
    var ageNumber = age*1;
    console.log(typeof age);//String
    console.log(typeof ageNumber);//Number

    3.Number()

    var age = "11";
    var ageNumber = Number(age);
    console.log(typeof age);//String
    console.log(typeof ageNumber);//Number

    三.字符串转化为数组

    1.split()

    var age = "1123";
    var ageArr = age.split("");
    console.log(typeof age);//String
    console.log(typeof ageArr);//Object
    console.log(ageArr);//["1", "1", "2", "3"]

    四.数组转化为字符串

    1.join()

    var age = ["1", "1", "2", "3"];
    var ageString = age.join("");
    console.log(typeof age);//Object
    console.log(typeof ageString);//String
    console.log(ageString);//1123

    2.toString()

    var age = ["1", "1", "2", "3"];
    var ageString = age.toString();
    console.log(typeof age);//Object
    console.log(typeof ageString);//String
    console.log(ageString);//1,1,2,3
  • 相关阅读:
    如何在linux系统中设置静态ip地址
    spring 学习
    java 反射机制和invoke方法
    CentoS7装机
    eclipse 添加jar包的方式
    No-args constructor for class does not exist. Register an InstanceCreator with G
    freemarker页面如何获取绝对路径basePath
    MySQL备份还原
    MySQL用户授权与权限
    CentOS7修改SSH远程连接端口
  • 原文地址:https://www.cnblogs.com/pcd12321/p/5337616.html
Copyright © 2011-2022 走看看