JavaScript中的字符串常用方法
- 字符串也可以像数组一样通过下标获取元素,也能通过str.length获取字符串长度
var str1 = '123456'
console.log(str1[2]) //3
console.log(str1.length) //6
2.trim() 作用是去掉字符串前后的空格,参数无,返回值为处理后的字符串
var str2 = ' 123 '
console.log(str2.length); //5
console.log(str2.trim().length); //3
3.split(arg) 作用是将字符串转成数组,参数(arg)为字符串分割标志,返回值为一个数组
var str3 = '123,441,5'
// 默认分割标志是 ,
console.log(str3.split()); //['123,441,5']
var str3 = '12;344;15'
console.log(str3.split(';')); //['12', '344', '15']
// 方法:我们可以通过以空串为分割标志,从而将字符串分割成一个个的字符数组
var str3 = '1234415'
console.log(str3.split('')); //['1', '2', '3', '4', '4', '1', '5']
4.substring(arg1,arg2) 作用截取字符串,第一个参数为截取开始的下标(包含),第二个参数为截取结束的下标(不包含),返回值为一个新的字符串(值为截取下来的内容)
var str4 = '123456'
var s = str4.substring(1,3)
console.log(s) //23
5.slice(arg1,arg2) 作用截取字符串,用法和substring类似,但它的参数可以为负数,第一个参数(必须)为截取开始的下标(包含),第二个参数(非必须)为截取结束的下标(不包含),当只有一个参数时,从该参数的位置一直截取到最后,返回值为一个新的字符串(值为截取下来的内容)
var str5 = '1234567'
console.log(str5.slice(1,4)); //234
//-1为最后一个数
console.log(str5.slice(1,-1)); //23456
console.log(str5.slice(1)); //234567
6.substr(arg1,arg2) 作用截取字符串,第一个参数为截取下标(包含)(必须)可以为负数,第二个参数为截取字符的个数(非必须),返回值为一个新的字符串(值为截取下来的内容)
var str6 = 'wqqrqf'
console.log(str6.substr(1,3)) //qqr
console.log(str6.substr(1)); //qqrqf
console.log(str6.substr(-2)); //qf
7.charAt(index) 作用是返回下标位置对应的字符,跟str[index]类似,只有一个参数为字符在字符串的下标,返回值为下标对应的字符,若没有则返回空。
var str7 = '123456'
console.log(str7.charAt(2)); //3
console.log(str7.charAt(9)); //
8.includes(arg) 作用是检查字符串包含指定字符串,只有一个参数为要检测的字符或字符串,返回值为布尔值,若包含返回true,否则返回false。
var str8 = 'qweqwe';
console.log(str8.includes('q')); //true
console.log(str8.includes('qwe')); //true
console.log(str8.includes('sss')); //false
console.log(str8.includes('')); //空串 true
console.log(str8.includes(' ')); //一个空格 false
9.indexOf(arg) 作用从左边开始检测字符串的位置(lastIndexOf(arg)功能相同,从右边开始检测),只有一个参数为需要检测的字符或者字符串,返回值为字符在字符串的下标位置,若没有就返回-1。
var str9 = 'qwertqweyu'
console.log(str9.indexOf('q')); //0
console.log(str9.lastIndexOf('q')); // 5
console.log(str9.indexOf('we')); //1
console.log(str9.lastIndexOf('we')); // 6
console.log(str9.indexOf('s')); // -1
console.log(str9.lastIndexOf('s')); // -1
console.log(str9.indexOf('')); //空串 0
console.log(str9.lastIndexOf('')); //空串 10
10.match(arg) 作用检测字符串是否匹配指定的字符串,参数只有一个,可以为字符或字符串,正则表达式,参数为字符串时,若匹配则返回一个数组,不匹配则返回null。参数为正则表达式时,不匹配则返回null,单个匹配返回只包含一个匹配值的数组,全局匹配返回包含多个匹配值的数组(若有多个的话)。
var str10 = 'wqeqwerq'
// 参数为字符串
console.log(str10.match('q')); //['q', index: 1, input: 'wqeqwerq', groups: undefined]
console.log(str10.match('qe')); //['qe', index: 1, input: 'wqeqwerq', groups: undefined]
console.log(str10.match('s')); //null
console.log(str10.match('')); //['', index: 0, input: 'wqeqwerq', groups: undefined]
// 参数为正则表达式
console.log(str10.match(/q/g)); // ['q', 'q', 'q']
console.log(str10.match(/r/g)); // ['r']
11.replace(arg1,arg2) 作用替换字符串,第一个参数为要替换的字符串,可以是字符串,也可以是正则表达式,第二个参数是要替换成的字符串,返回值为替换后的字符串。
var str11 = '1-2-3-4-5-6'
console.log(str11.replace('1','ww')); //ww-2-3-4-5-6
console.log(str11.replace(/-/g,'+')); //1+2+3+4+5+6
12.toLowerCase() 作用转换成为小写字母(toUpperCase()作用与其相反 作用是转换成为大写字母)不用参数,返回值为一个新的字符串。
var str12 = 'wQFddcWW'
console.log(str12.toLowerCase()); //wqfddcww
console.log(str12.toUpperCase()); //WQFDDCWW