声明一个数组
var arr = [1,2,5,3];
在数组尾部添加元素
arr.push(49,555); // arr --> [1,2,5,3,49,555] push()返回值为添加元素后的数组的长度
数组尾部删除一个元素
arr.pop(); //pop() 删除尾部的一个元素返回的是被删除的元素 arr --> [1,2,5,3,49]
在数组头部添加元素
arr.unshift(2,8) //返回值为添加元素后数组的长度,,
在数组头部删除一个元素
arr.shift() //返回值为被删除的元素
数组的splice方法
splice()直接删除了元素使其length改变
删除元素,splice(para1,para2);para1 开始项的位置,如果为负数则加上数组长度后作为para1的值,para2 删除的项数,如果为负数或0则不进行删除操作。未写则直到末尾返回的为被删除的元素
增加元素,splice(para1,0,para2...) para1开始项位置,para2及之后为要添加的元素
替换元素,splice(para1,para2,para3....) para1 开始项位置,para2删除的元素个数,para3之后的为要添加的元素
复制数组slice(start,end);
reverse() //反转数组
sort() //排序 接收一个比较函数作为数组
concat() //基于当前数组中的所有项创建一个新的数组。具体来说就是先创建一个当前数组副本,然后将接收到的参数添加到这个副本的末尾最后返回新构建的数组
join('0');将数组以指定的字符为间隔装换为字符串。[1,2,3]; 装换后。102030
。。字符串
substring(start,end);复制字符串。。
charAt(); 以单字符形式返回指定位置的字符
charCodeAt(); 返回指定位置字符的字符编码。
concatenate(); 用于拼接字符串,实践中多用加号
slice(),,substr(),substring();
var str = "hellow world";
str.slice(3); //"lo world"
str.substring(3); //"lo world"
str.substr(3); //"lo world"
str.slice(3,7); //"lo w"
str.substring(3,7); //"lo w"
str.substr(3,7); //"lo worl" 第二个参数指长度
当参数为负数时
str.slice(-3); //"rld" 加上字符串长度
str.substring(-3); //"hello world" 负数被转为0
str.substr(-3); //"rld" 加上字符串长度
str.slice(3,-4); //"lo w" 加上字符串长度
str.substring(3,-4); //"hel" 负值被转为零,小数被当做开始,大数为结束等于调用了,substring(0,3)
str.substr(3,-4); //"" 第二个参数被转为零
str.indexOf('0');<span style="white-space:pre"> </span>//4<span style="white-space:pre"> </span>从开头向后搜索
str.lastIndexOf('0');<span style="white-space:pre"> </span>//7<span style="white-space:pre"> </span>从结尾向前搜索
str.indexOf("0",6);<span style="white-space:pre"> </span>//从指定位置开始搜索
str.lastIndexOf("0",6);<span style="white-space:pre"> </span>//从指定位置开始搜索
str.trim();<span style="white-space:pre"> </span>//删除开头结尾的空格返回一个新字符串,原字符串不变
str.toLocaleUpperCase();<span style="white-space:pre"> </span>//"HELLOW WORLD"
str.toUpperCase();<span style="white-space:pre"> </span>//"HELLOW WORLD"
str.toLocaleLowerCase();<span style="white-space:pre"> </span>//"helow world"
str.toLowerCase();<span style="white-space:pre"> </span>//"helow world"
str.split("o",2);
//以指定的字符分割字符串返回数组,第二个参数为数组长度