今天所学到的方法:
(1)splice():
向/从数组中添加/删除项目,然后返回被删除的项目。
例如:
var arr = ["George","John","Thomas","James","Adrew","Martin"]
console.log(arr) // ["George","John","Thomas","James","Adrew","Martin"]
arr.splice(2,0,"William") //添加
console.log(arr); //["George","John","William","Thomas","James","Adrew","Martin"]
arr.splice(2,1) //删除
console.log(arr); //["George","John","Thomas","James","Adrew","Martin"]
(2)split():
用于把一个字符串分割成字符串数组。
例如:
var str = 'abbcdw';
var a = str.split(''); //''为分割每一个字符
console.log(a) //["a", "b", "b", "c", "d", "w"]
(3)join():
var str = 'abbcdw';
var a = str.split(''); //''为分割每一个字符
console.log(a) //["a", "b", "b", "c", "d", "w"]
console.log(a.join('')); // abbcdw
(4)toUpperCase():
将字符串中的小写字符转换成大写
//对数组使用
var arr = ["George","John","Thomas","James","Adrew","Martin"]
arr[1] = arr[1].toUpperCase();
console.log(arr[1]); //JOHN
//对字符串使用
var str = 'George'
str = str.toUpperCase();
console.log(str); //GEORGE
//只转字符串的其中一个字符
var str = "Thomas";
var a = str.split('');
a[2] = a[2].toUpperCase();
str = a.join('');
console.log(str); // ThOmas
(5)toLowerCase():
将字符串中的大写字符转换成小写
//对数组使用
var arr = ["George","John","Thomas","James","Adrew","Martin"]
arr[1] = arr[1].toLowerCase();
console.log(arr[1]); //john
//对字符串使用
var str = 'George'
str = str.toLowerCase();
console.log(str); //george
//只转字符串的其中一个字符
var str = "Thomas";
var a = str.split('');
a[0] = a[0].toLowerCase();
str = a.join('');
console.log(str); // thomas
(6)indexOf():
返回某个指定的字符串值在字符串中首次出现的位置
- indexOf() 方法对大小写敏感!
- 如果要检索的字符串值没有出现,则该方法返回 -1。
var str="Hello world!"
document.write(str.indexOf("Hello")) //0
document.write(str.indexOf("World")) //-1 不存在
document.write(str.indexOf("world")) //6
(7) replace():
字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。
var str="Visit Microsoft!"
console.log(str.replace(/Microsoft/, "W3School")) //W3School