首页  >  文章  >  web前端  >  总结字符串、数组及Math的常见方法

总结字符串、数组及Math的常见方法

巴扎黑
巴扎黑原创
2017-07-18 18:04:561299浏览

1.字符串方法
    str.charAt()  //在xx位置处字符是什么
    str.toLowerCase()  //全转为小写字符
    str.toUpperCase()  //全转为大写字符
    str.indexOf()  //xx字符首次出现的位置
    str.laseIndexOf()  //xx字符最后出现的位置
    str.substring()  //字符串从哪个位置截取到哪个位置,原数组不变
    str.split()  //字符串以xx字符分割为数组

var arr = 'If you must say yes, say it with an open heart.';
console.log(arr.charAt(3));//yconsole.log(arr.toLowerCase());//if you must say yes, say it with an open heart.console.log(arr.toUpperCase());//IF YOU MUST SAY YES, SAY IT WITH AN OPEN HEART.console.log(arr.indexOf('y'));//3console.log(arr.lastIndexOf('y'));//23console.log(arr.subString(4,10));//ou musconsole.log(arr.split(" "));//["If", "you", "must", "say", "yes,", "say", "it", "with", "an", "open", "heart."]

 

  1.substrng(start开始位置的索引,end结束位置索引) 截取的位置不包含结束位置的字符,只写一个参数表示从开始位置截取到最后
                var str='abcdefg';  
                
                str.substring(1)  //得到bcdefg    str.substring(1,3)  //得到bc

                输入负值时将负值变为0,哪个较小作为开始位置

                str.substing(-1,1)  =>str.substring(0,1) //a
                str.substring(1,-2) =>str.substring(0,1) //a

        2.slice(start开始位置索引,end结束位置索引) 基本和substring相似,区别在参数为负数。

            var str='abcdefg'; 

            str.slice(1)   //bcdefg           str.substring(1,3) // bc

            输入负值时 值与字符串的长度相加

            str.slice(-1)          =>str.slice(6)    //g
                str.slice(1,-2) =>str.slice(1,5)  //bcde
                str.slice(-2,-1)=>str.slice(5,6)  //f
                值绝对值大于字符串的长度时变为 0

                str.slice(-22) =>str.substring(0)  //abcdefg

        第二个参数绝对值大于字符串的长度时,返回''

    3.substr(start开始位置索引,end需要返回的字符个数)

            var str='abcdefg'; 

            str.substr(1)  //bcdefg            str.substr(1,1)  //b

        输入负值时 start参数与字符串的长度相加 ,end为负时参数变为0

        str.substr(-1) =>str.substr(6)//g                 

        str.substr(-2,-3)  // ''


2.数组方法 
    arr.push() //在数组后面添加元素,返回数组长度,原数组改变
    arr.unshift() //在数组前面添加元素,返回数组长度,原数组改变
    arr.pop()  //删除数组最后一个元素,返回最后一个元素,原数组改变
    arr.shift()  //删除数组第一个元素,返回第一个元素,原数组改变
    arr.join()  //以xx字符把数组各元素连接成字符串,原数组不变
    arr.splice(start,num,args)  //从start位置起,把num个元素,换成args=a,b,c,d,e,原数组改变
    arr.reverse()  //反转数组,原数组改变
    arr.concat()  //拼接数组,原数组不变
    arr.sort()  //从小到大排序,原数组改变

var arr = [1,2,'three',4,5];var arr1 = ['love',99] ;
console.log(arr.push(6));//6console.log(arr.unshift(0));//7console.log(arr.pop());//6console.log(arr.shift());//0console.log(arr.join('-'));//1-2-three-4-5console.log(arr.splice(2,1,3,4));//["three"]console.log(arr);//[1, 2, 3, 4, 4, 5]console.log(arr.reverse());//[5, 4, 4,3, 2, 1]console.log(arr.concat(arr1));//[5, 4, 4, 3, 2, 1, "love", 99]console.log(arr.sort());//[1, 2, 3, 4, 4, 5]

3.slice(startIndex, endIndex) //截取startIndex开始的(endIndex-startIndex)个数据,字符串数组都可以,如果endIndex为负,则相当于(endIndex+原数据长度),操作后原数据不变

var  arr = [1,'two',3];var arr1 = 'love';
console.log(arr.slice(1,-1));//['two']console.log(arr.slice(1,3));//["two", 3]console.log(arr1.slice(1,3));//ov

 

 

4.数学方法

    Math.random()  // 0~1随机数
    Math.pow(x,y)  // x的y次方
    Math.sqrt(x)  // x开2次方
    Math.abs()  // 绝对值
    Math.floor(x)  // 少于等于x的最大整数
    Math.ceil(x)  // 大于等于x的最小整数
    Math.round(x)  // 四舍五入
    Math.max(x, y, z)  // 返回最大值
    Math.min(x, y, z)  // 返回最小值

     var a = 3.4;     var b = 6.6;
     console.log(Math.random());//0-1随机数 console.log(Math.pow(a,3));//39.3--a的3次方 console.log(Math.sqrt(a));//1.84--开2次方 console.log(Math.abs(a));//绝对值 console.log(Math.floor(a));//3--少于等于a的最大整数 console.log(Math.ceil(a));//4--大于等于a的最小整数 console.log(Math.round(a));//3--四舍五入 console.log(Math.max(a,b,1));//6.6--返回最大值 console.log(Math.min(a,b,1));//1--返回最小值

        

      

以上是总结字符串、数组及Math的常见方法的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn