码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-07-16 19:21:05      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:ever   tool   span   tar   复制   last   src   连接   反转   

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));//y
console.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‘));//3
console.log(arr.lastIndexOf(‘y‘));//23
console.log(arr.subString(4,10));//ou mus
console.log(arr.split(" "));//["If", "you", "must", "say", "yes,", "say", "it", "with", "an", "open", "heart."]
技术分享

 

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));//6
console.log(arr.unshift(0));//7
console.log(arr.pop());//6
console.log(arr.shift());//0
console.log(arr.join(‘-‘));//1-2-three-4-5
console.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常见方法

标签:ever   tool   span   tar   复制   last   src   连接   反转   

原文地址:http://www.cnblogs.com/zuiai/p/7191224.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!