1.字符串操作方法
* 返回取到的那个字符,类型为字符串
var str='kaivon';
var s1=str.charAt(1);
console.log(s1); //a
var s2=str.charAt();
console.log(s2); //k
var s3=str.charAt(10);
console.log(s3); //空
* 返回字符对应的Unicode编码
var str='kaivon';
var s1=str.charCodeAt(1);
console.log(s1); //97
var s2=str.charCodeAt();
console.log(s2); //107
var s3=str.charCodeAt(10);
console.log(s3); //NaN
* 返回一个数字,这个数字代表字符在整个字符串里的真实下标,如果没找到的话就返回-1
var str='kaivaokn';
var s1=str.indexOf('a');
console.log(s1); //1
var s2=str.indexOf('k',1);
console.log(s2); //6
var s3=str.indexOf('b');
console.log(s3); //-1
* 返回一个数字,如果找到的话就返回这个字符的真实下标,如果没找到返回-1
var str='kaivokn';
var s1=str.lastIndexOf('a');
console.log(s1); //1
var s2=str.lastIndexOf('k',5);
console.log(s2); //5
var s3=str.lastIndexOf('b');
console.log(s3); //-1
* 返回截取到的那个字符串,原字符串没有变化
* 与slice的区别* 1、slice起始位置不能大于结束位置,而substring起始位置可以大于结束位置 (会自动调大小)* 2、slice可以放负数,而substring是不能放负数的
var str='kaiovn';
var s1=str.slice();
console.log(s1); //kaiovn
var s2=str.slice(3);
console.log(s2); //ovn
var s3=str.slice(2,4);
console.log(s3); //io
var s4=str.slice(-3,-2);
console.log(s4); //o
console.log(str); //kaiovn
* 返回一个数组,原字符串没有变化
var str='kaivon';
var s1=str.split();
console.log(s1); //["kaivon"]
var s2=str.split('');
console.log(s2); //["k", "a", "i", "v", "o", "n"]
var s3=str.split(' ');
console.log(s3); //["kaivon"]
var s4=str.split('a');
console.log(s4); //["k", "ivon"]
var s5=str.split('i',1);
console.log(s5); //["ka"]
console.log(str); //kaivon
* 返回截取到的字符串,原字符串不变
var str='kaivon';
var s1=str.substr();
console.log(s1); //kaivon
var s2=str.substr(2);
console.log(s2); //ivon
var s3=str.substr(3,2);
console.log(s3); //vo
console.log(str); //kaivon
* 返回字符串转成小写的结果,原来的字符串不有变化
var str='KaiVoN';
var s1=str.toLowerCase();
console.log(s1); //kaivon
console.log(str); //KaiVoN
* 返回字符串转成大写的结果,原字符串不变
var str='kaivon';
var s1=str.toUpperCase();
console.log(s1); //KAIVON
console.log(str); //kaivon
* 返回字符串去掉首尾空格的结果,原字符串不变
var str=' kaivon ';
var s1=str.trim();
console.log(s1); //kaivon
console.log(str); // kaivon
* 一种类似JS中的对象格式的字符串(JSON它是一个字符串,只不过内容长得像对象)
* 每个数据之间用逗号隔开
var j1="[1,2,3]";
var j2='{"name":"老王","age":30}';
console.log(j1,typeof j1); //[1,2,3] string
console.log(j2,typeof j2); //{"name":"老王","age":30} string
* 把一个JSON格式的字符串转成对象,返回转过后的对象
var j1="[1,2,3]";
var j2='{"name":"老王","age":30}';
var obj1=JSON.parse(j1);
console.log(obj1,typeof obj1); //[1, 2, 3] "object"
var obj2=JSON.parse(j2);
console.log(obj2,typeof obj2); //{name: "老王", age: 30} "object"
var j3='{name:"kaivon"}';
var obj3=JSON.parse(j3); //报错 key没有加引号
var j4='['aow','shw','dwn']';
console.log(JOSN.parse(j4)); //报错 因为元素只是单引号,需要改成双引号
* 如果对象当中的key没有加引号,也可以转换,转换后的结果会自动给它加上引号
var obj1=[1,2,3];
var obj2={"name":'老王',"age":30};
var obj3={name:'老王',"age":30};
var obj4=['ad','gf','fsd'];
console.log(JSON.stringify(obj4)); // ["ad","gf","fsd"]
var s1=JSON.stringify(obj1);
console.log(s1); //[1,2,3]
var s2=JSON.stringify(obj2);
console.log(s2); //{"name":"老王","age":30}
var s3=JSON.stringify(obj3);
console.log(s3); //{"name":"老王","age":30}
* 返回参数向上取整后的结果
console.log(Math.ceil(0.1)); //1
console.log(Math.ceil(-10.8)); //-10
console.log(Math.ceil(10)); //10
* 返回参数向下取整后的结果
console.log(Math.floor(0.1)); //0
console.log(Math.floor(-10.8)); //-11
console.log(Math.floor(10)); //10
* 返回参数四舍五入后的结果
console.log(Math.round(3.15)); //3
console.log(Math.round(4.5)); //5
console.log(Math.round(-10.87)); //-11
* 返回参数取绝对值后的结果
console.log(Math.abs(10)); //10
console.log(Math.abs(-11)); //11
console.log(Math.abs(5-7)); ///2
* Math.random()
0 ~ x-1 : Math.floor(Math.random()*x)
//0-10
console.log(Math.round(Math.random()*10));
//5-7
console.log(Math.round(Math.random()*(7-5) + 5));
* 返回数组添加后的长度,原数组变化,变成添加后的数组
var color=['red','green','blue','pink'];
var len=color.push('white');
console.log(color); //['red','green','blue','pink','white'];
console.log(len); //5
* 返回数组添加后的长度,原数组变化,变成添加后的数组
var color=['red','green','blue','pink'];
var len=color.unshift('white');
console.log(color); //['white','red','green','blue','pink']
console.log(len); //5
* 返回被删除的那个数据,原数组变化,变成删除后的数组
var color=['red','green','blue','pink'];
var del=color.pop();
console.log(del); //pink
console.log(color); //["red", "green", "blue"]
* 返回被删除的那个数据,原数组变化,变成删除后的数组
var color=['red','green','blue','pink'];
var del=color.shift();
console.log(del); //red
console.log(color); //["green", "blue", "pink"]
* array 数组本身
var color=['red','green','blue','pink'];
color.forEach(function(currentValue,index,array){
console.log(currentValue,index,array)
});
* 返回排序后的数组,原数组变了,变成排序后的数组
var arr=[4,2,7,8,3,1,10];
arr.sort(function(a,b){
return a-b;
})
console.log(arr); //[1, 2, 3, 4, 7, 8, 10]
var arr1=[4,2,7,8,3,1,10];
arr1.sort(function(a,b){
return b-a;
});
console.log(arr1); //[10, 8, 7, 4, 3, 2, 1]
//随机排序
var arr2=[4,2,7,8,3,1,10];
arr2.sort(function(){
return 0.5-Math.random();
})
console.log(arr2);
//没有参数
var arr3=[4,2,7,8,3,1,10];
arr3.sort();
console.log(arr3); //按照字符对应的unicode编码号
* 返回颠倒后的数组,原数组变化了,变成颠倒后的数组了
var color=['red','green','blue','pink'];
var a=color.reverse();
console.log(a); //["pink", "blue", "green", "red"]
console.log(color); //["pink", "blue", "green", "red"]
* 合并后的新数组,原数组是不变的
var color=['red','green','blue','pink'];
var newColor=color.concat('yellow');
console.log(newColor); //["red", "green", "blue", "pink", "yellow"]
console.log(color); //["red", "green", "blue", "pink"]
var newColor2=color.concat(['yellow','gold']);
console.log(newColor2); //["red", "green", "blue", "pink", "yellow", "gold"]
var newColor3=color.concat(1,2,3);
console.log(newColor3); //["red", "green", "blue", "pink", 1, 2, 3]
* 返回数组转成的字符串,原数组不变
var color=['red','green','blue','pink'];
var newColor=color.join();
console.log(newColor); //red,green,blue,pink
console.log(color); //["red", "green", "blue", "pink"]
var new1=color.join('');
console.log(new1); //redgreenbluepink
var new2=color.join(' ');
console.log(new2); //red green blue pink
var new3=color.join('&');
console.log(new3); //red&green&blue&pink
* 返回截到的数组,原数组不会变
var color=['red','green','blue','pink','yellow'];
var n1=color.slice();
console.log(n1); //["red", "green", "blue", "pink", "yellow"]
var n2=color.slice(2);
console.log(n2); //["blue", "pink", "yellow"]
var n3=color.slice(3,4);
console.log(n3); //["pink"]
console.log(color); //["red", "green", "blue", "pink", "yellow"]
* 原数组变化了,变成删除后剩下的内容
var color=['red','green','blue','pink','yellow'];
var c1=color.splice();
console.log(c1); //[]
console.log(color); //["red", "green", "blue", "pink", "yellow"]
var c2=color.splice(3);
console.log(c2); //["pink", "yellow"]
console.log(color); //["red", "green", "blue"]
var color1=['red','green','blue','pink','yellow'];
var c3=color1.splice(1,3);
console.log(c3); //["green", "blue", "pink"]
console.log(color1); //["red", "yellow"]
var color2=['red','green','blue','pink','yellow'];
var c4=color2.splice(1,2,'white','black');
console.log(c4); //["green", "blue"]
console.log(color2); //["red", "white", "black", "pink", "yellow"]
var color3=['red','green','blue','pink','yellow'];
var c5='a,b,c';
var c6=color3.splice(3,2,c5);
console.log(c6); //['pink',yellow]
console.log(color3); //["red", "green", "blue", "a,b,c"]