JavaScript一些简单的内置对象(String,Math,Date)

String属性、Math、Date的简单使用

String对象的属性
  • length: 字符串对象长度

  • charAt(index) 返回指定位置的字符,也可以直接:str[index]

  • indexOf(str,index) 查找某个指定的字符串在字符中首次出现的位置,index开始查找的位置,str需要查找的字符串,没有找到返回-1,
  • substring(index1,inex2)返回指定索引index1和index2之间的字符串,并且包括索引index对应的字符,不包括index2对应的字符。[index1,index2)位置
  • split(str) 将字符串分割为字符串数组,以str分割
  • toLowerCase()把字符转换为小写
  • toUpperCase()转换成大写
        <script>
            var str = "JavaScript";
            var strLength = str.length;
            var str2 = "a good man a real man";
            //打印10
            console.log(strLength);
            console.log(str.charAt(2));
            console.log(str[2]);
            //查找va首次出现的位置,从1的位置开始查找
            console.log(str.indexOf("va",1));
            console.log(str.substring(0,4));
            console.log(str2.split(" "));
        </script>

Math对象

  • ceil() 对数进行上舍入,可以看做为取整后+1
  • floor() 对数进行下舍入,可以看做取整后-1
  • round() 4舍5入
  • random 返回0~1之间的随机数,random值为[0,1);
<script>
            //打印:26
            console.log("ceil(25.3)"+Math.ceil(25.3));
            //打印:-25
            console.log("ceil(-25.3)"+Math.ceil(-25.3));
            console.log("floor(25.8)"+Math.floor(25.8));
            console.log("floor(-25.8)"+Math.floor(-25.8));
            console.log("round(25.6)"+Math.round(25.6));
            console.log("round(-25.6)"+Math.round(-25.6));
            console.log("0-16的随机数"+Math.random()*16);

        </script>

Date对象的的方法
  • getDate():返回一个月中的每一天其值介于1~31。
  • getDay(): 返回星期中的某一天,其值0~6,0代表星期天
  • getHours():返回小时数,值0~23之间,0代表24
  • getMinutes():返回分钟数,值0~59
  • getSeconds():返回秒数,值0~59
  • getMonth():返回月份,值0~11,0代表12月份
  • getFullYear():返回年份,值为4位数
    <script>
            var taday = new Date();
            var year = taday.getFullYear();
            var mm = taday.getMonth();
            var dd = taday.getDate();
            var hh = taday.getHours();
            var mint = taday.getMinutes();
            var ss = taday.getSeconds();

            document.write(year+"年"+(mm+1)+"月"+dd+"日"+"--"
            +hh+":"+mint+":"+ss);


        </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值