5. 常用的字符串、数组、Math方法

1. 字符串
str.charAt(index) ——  找不到,返回空字符串
str.indexOf(找东西) ——  找到了返回一个下标,没找到返回-1
str.lastIndexOf()  ——  从后面往前找    同上
str.substring(beginIndex[, endIndex])  ——   截取字符串,不包含最后一位
str.split(切规则)  —— 返回一个数组
str.toUpperCase() —— 把字符串转化为大写
str.toLowerCase() —— 把字符串转化为小写
 
2. 数组
arr.push(value)  —— 数组保存在数组的最后一位
arr.pop() —— 删除一个数,删除数组的最后一位
arr.unshift(value) —— 往数组的第一位添加数据
arr.shift() —— 删除数组的第一位
arr.split(起始位置, 删除的个位, 元素1, 元素2... 元素N)
arr.reverse() —— 数组反转
arr.concat(arr1, arr2...) —— 数组连接
arr.join(连接的规则) —— 返回的是一个字符串
 
arr.sort() —— 对数组元素( json )进行排序
sort(function(n1, n2) {
    return n1 - n2;
});
sort(function(json1, json2) {
    return json1.xx - json2.xx;
});
 
注:pop    shift都有返回值
 
3. Math
Math.random() —— 返回介于0~1之间的随机数
Math.floor() —— 向下取整 -1
Math.ceil() —— 向上取整 +1
Math.abs() —— 绝对值 不会取整
Math.round() —— 四舍五入    floor(value + 0.5)
Math.pow(基数, 幂) —— 幂
Math.sqrt() —— 开平方
Math.max/min —— 最大最小值
posted @ 2015-09-14 16:30  Sampson1207  阅读(158)  评论(0编辑  收藏  举报