zoukankan      html  css  js  c++  java
  • Js--String、Date、Array对象

    /*
    * String 对象
    属性 length
    方法
    */
    //String的length属性
    var strL = "abcde";
    document.write("<br/>");
    document.write("属性:" + strL.length);
    //String的方法
    document.write("<br/>");
    document.write("方法:" + strL.bold());
    document.write("<br/>");
    document.write(strL.fontcolor("red"));
    document.write("<br/>");
    document.write(strL.fontsize(7));
    document.write("<br/>");
    // sub sup下标

    document.write("<br/>");
    var str1 = "hello";
    var str2 = "world";
    document.write("字符串链接:" + str1.concat(str2));
    document.write("<br/>");
    document.write("返回指定位置字符串,位置没有值返回null:" + str2.charAt(0));
    document.write("<br/>");
    document.write("返回指定位置字符串的索引,值没有返回-1:" + str2.indexOf("w"));
    document.write("<br/>");
    var str3 = "a-b-c-d";
    var arr1 = str3.split("-");
    document.write("split方法切割,切分字符串,成数组:" + arr1.length);
    document.write("<br/>");
    var str4 = "abcd";
    document.write("replace替换,把a替换成e" + str4.replace("a", "e"));
    document.write("<br/>");
    document.write("substr," + str4.substr(1, 2));//bc 从第1位开始向后截取2位
    document.write("<br/>");
    document.write("substring:" + str4.substring(1, 2));//b 从第几位开始到第几位结束,不包含最后的那位[1,2)

    //数组的3种创建方式
    var arr21 = [ 1, 2, 3 ];
    var arr22 = new Array(3);
    var arr23 = new Array(1, 2, 3);

    /**js的Array对象
    concat()
    join()
    push()
    pop()
    reverse()
    */
    var arr11 = [ 1, 2, 3 ];
    document.write("<hr/>");
    document.write(arr11.length);
    document.write("<br/>");
    document.write("concat():数组的链接" + arr11.concat());
    document.write("<br/>");
    var arr12 = new Array(3);
    arr12[0] = "a";
    arr12[1] = "b";
    arr12[2] = "c";
    document.write("join():使用指定字符分割数组:" + arr12.join("-"));
    document.write("<br/>");
    document.write("push():在数组末尾添加一个元素,并且返回新的数组长度:" + arr12.push("d"));
    document.write("<br/>");
    var arr13 = [ "aaa", "bbb", "ccc" ];
    var arr14 = [ "www", "qqq" ];
    document.write("在一个数组里用push方法添加一个数组:" + arr13.push(arr14));
    document.write("<br/>");
    //使用push给数组添加一个数组,他会把这个数组当成元素整体,作为字符串添加到数组中
    for (var i = 0; i < arr13.length; i++) {
    document.write(arr13[i]);
    document.write("<br/>");
    }

    document.write("<br/>");
    var arr14 = [ "zhangsan", "lisi", "wangwu", "zhaoliu" ];
    document.write("old length:" + arr14.length);
    document.write("<br/>");
    document.write("pop()删除最后一个元素,并且返回最后一个元素:" + arr14.pop());
    document.write("<br/>");
    document.write("new length:" + arr14.length);
    document.write("<br/>");
    document.write("new arr:" + arr14);

    document.write("<br/>");
    document.write("reverse():颠倒数组中的顺序:" + arr14.reverse());

    /**Date对象
    toLocaleString:把日期转换成习惯的日期格式
    getFullYear:得到当前年四位
    getMonth:得到当前月,返回值是0-11月,得到准值+1
    getDay:得到当前星期,返回值是0-6,星期日是0表示
    getDate:得到当前的天,1-31
    getHours:获得当前的小时
    getMinutes():得到当前的分钟
    getSeconds():得到当前的秒
    getTime():得到毫秒数:返回的是1970-1-1 至今的毫秒数 应用场景:使用毫秒数处理缓存的效果(没有缓存) 例:www.baidu.com?毫秒数
    */
    var date = new Date();
    document.write("<hr/>");
    document.write("本地日期格式:" + date);//获取当前的时间
    document.write("<br/>");
    document.write("转成习惯的日期格式:" + date.toLocaleString());
    document.write("<br/>");
    document.write("获取当前的年的方法:getFullYear():" + date.getFullYear());
    document.write("<br/>");
    document.write("获取当前的月的方法:getMonth():" + (date.getMonth() + 1));
    document.write("<br/>");
    document.write("获得当前的星期:getDay():" + date.getDay());
    document.write("<br/>");
    document.write("获得当前的日:getDate():" + date.getDate());
    document.write("<br/>");
    document.write("获得当前的小时:getHours():" + date.getHours());
    document.write("<br/>");
    document.write("获得当前的分钟:getMinutes():" + date.getMinutes());
    document.write("<br/>");
    document.write("获得当前的秒:getSeconds():" + date.getSeconds());
    document.write("<br/>");
    document.write("获得毫秒数:getTime()" + date.getTime());//返回的是1970-1-1 至今的毫秒数 应用场景:使用毫秒数处理缓存的效果(没有缓存) 例:www.baidu.com?毫秒数

    /**Math对象里全是静态方法,使用Math.方法名();
    ceil():向上舍入
    floor():向下舍入
    round():四舍五入
    random():随机数
    max(x,y):最大值
    min(x,y):最小值
    pow(x,y):x的y次幂、
    */
    document.write("<hr/>");
    var mm=10.4;
    document.write("ceil():向上舍入:"+Math.ceil(mm));
    document.write("<br/>");
    document.write("floor():向下舍入:"+Math.floor(mm));
    document.write("<br/>");
    document.write("round()四舍五入"+Math.round(mm));
    document.write("<br/>");
    document.write("random()随机数"+Math.floor(Math.random()*10));//获得0-9的随机数
    document.write("<br/>");
    document.write("pow(x,y):x的y次幂:"+Math.pow(2,5));

  • 相关阅读:
    android学习日记19--四大组件之BroadcastReciver(广播接收者)
    android学习日记19--四大组件之Services(服务)
    android学习日记18--Adapter简介
    android学习日记17--Gallery(画廊视图)
    android学习日记16--GridView(网格视图)
    android学习日记15--WebView(网络视图)
    android学习日记14--网络通信
    android报错及解决2--Sdcard进行文件的读写操作报的异常
    android学习日记13--数据存储之File存储
    自定义跨浏览器的事件处理程序
  • 原文地址:https://www.cnblogs.com/xiqoqu/p/9058289.html
Copyright © 2011-2022 走看看