一、对于字符串的方法
var a="abcde" ; var b="houxiaoru" ;
1.concat() 拼接两个字符串,返回一个新字符串
var c= a.concat(b);
alert(c);
结果: abcdehouxiaoru
2.indexOf () 返回字符串中某个字符的索引值 从左往右数
alert(b.indexOf("x",0));
结果:3
注:若第二个值比其索引值大,返回负一。例如:alert(b.indexOf("x",5)); 结果: -1
lastIndexOf() 返回某字符串的索引位 从右往左数
3.charAt() 返回索引值为x的字符
alert(b.charAt(5));
结果:a
4.subString() 输出该字符串中索引值从n-m的所有元素 不包括m
alert(b.substring(0,3));
结果: hou
注:alert(b.substring(4)); (从索引值为四开始输出)
结果:iaoru
5.substr() 从索引值为n的元素开始,输出m位
alert(b.substr(3,2));
结果:xi
注:alert(b.substr(5)); (从索引值为五开始输出)
结果:aoru
6.replace() 将字符串中的某些字符改为另外的一些字符
alert(b.replace("xi","nnn"));
结果: hounnnaoru
7.slice() 同substring
alert(b.slice(3,6));
结果: xia
8.split() 将字符串转化为数组
console.log(b.split(""));
结果: [h,o,u,x,i,a,o,r,u]
9.toUpperCase() 字符转大写
toLowerCase() 字符转小写
console.log(b.toUpperCase());
结果: HOUXIAORU
二、对与数组的方法
var arr=[1,54,12,43,20];
10. shift() 删除第一个元素 并返回该元素 对原数组有影响
console.log(arr.shift());
结果: 1
11. pop() 删除最后一个元素 并返回该元素
console.log(arr.pop());
结果: 20
12. unshift() 在数组前追加一个值,返回数组长度 对数组有影响
console.log(arr.unshift(2));
结果:6
13.push() 在数组后追加一个值,返回数组长度 对数组有影响
console.log(arr.push(5));
结果: 6;
14.slice() 索引值从n-m的元素 不包括m
splice() 索引值从n-m的元素 包括m
console.log(arr.slice(2,4));
结果:12 43
console.log(arr.splice(2,4));
结果:12 43 20
15.join() 将数组变为字符串
console.log(a.join(","));
结果:1,4,2,7,3,9