字符串常用的方法
1.charAt()
返回指定索引位置处的字符。
类似于数组用中括号获取相应下标位置的数据。
var str = 'abcdefg'
console.log(str.charAt(2)) // 输出 'c'
console.log(str[2]) // 输出 'c'2
3
2.concat()
类似数组的concat(),用来返回一个合并拼接两个或两个以上字符串。
原字符串不变。
const str1 = 'abcdefg'
const str2 = '1234567'
const str3 = str1.concat(str2)
console.log(str3) // 输出 'abcdefg1234567'2
3
4
使用解构
const a =[...str1 , ...str2].join("")
console.log(a);2
3.indexOf()、lastIndexOf()
indexOf,返回一个字符在字符串中首次出现的位置
lastIndexOf 返回一个字符在字符串中最后一次出现的位置。
const str = 'abcdcefcg'
console.log(str.indexOf('c')) // 输出 '2'
console.log(str.lastIndexOf('c')) // 输出 '7'2
3
4.slice()
提取字符串的片断,并把提取的字符串作为新的字符串返回出来。
原字符串不变。
const str = 'abcdefg'
console.log(str.slice()) // 输出 'abcdefg', 不传递参数默认复制整个字符串
console.log(str.slice(1)) // 输出 'bcdefg',传递一个,则为提取的起点,然后到字符串结尾
console.log(str.slice(2, str.length-1)) // 输出'cdef',传递两个,为提取的起始点和结束点2
3
4
5.split()
使用指定的分隔符将一个字符串拆分为多个子字符串,并返回数组
原字符串不变。
const str = 'A*B*C*D*E*F*G'
console.log(str.split('*')) // 输出 ["A", "B", "C", "D", "E", "F", "G"]2
6.substr(), substring()
这两个方法的功能都是截取一个字符串的片段,并返回截取的字符串。
substr和substring它们的参数一,都是一样的功能,截取的起始位置。这两个方法不同的地方就在于参数二:
substr的参数二是截取返回出来的这个字符串指定的长度
substring的参数二是截取返回这个字符串的结束点,并且不包含这个结束点。
注意事项:
substr的参数二如果为0或者负数,则返回一个空字符串,如果未填入,则会截取到字符串的结尾去。
substring的参数一和参数二为NAN或者负数,那么它将被替换为0。
const str = 'ABCDEFGHIJKLMN'
console.log(str.substr(2)) // 输出 'CDEFGHIJKLMN'
console.log(str.substring(2)) // 输出 'CDEFGHIJKLMN'
console.log(str.substr(2, 9)) // 输出 'CDEFGHIJK'
console.log(str.substring(2, 9)) // 输出 'CDEFGHI'2
3
4
5
6
7.match()
match()方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配,并返回一个包含该搜索结果的数组。
const str = '2018年结束了,2019年开始了,2020年就也不远了'
const reg = /\d+/g // 这里是定义匹配规则,匹配字符串里的1到多个数字
console.log(str.match(reg)) // 输出符合匹配规则的内容,以数组形式返回
// ['2018', '2019', '2020']
console.log(str.match('20')) // 不使用正则
// [
// '20',
// index: 0,
// input: '2018年结束了,2019年开始了,2020年就也不远了',
// groups: undefined
// ]2
3
4
5
6
7
8
9
10
11
12
13
注意事项:
如果match方法没有找到匹配,将返回null。
如果找到匹配,则 match方法会把匹配到以数组形式返回
如果正则规则未设置全局修饰符g,则 match方法返回的数组有两个特性:input和index。
index属性包含了在整个被搜索字符串中匹配的子字符串的位置。
input属性包含整个被搜索的字符串。
8.replace()
replace接收两个参数
参数一 是需要替换掉的字符或者一个正则的匹配规则
参数二 是需要替换进去的字符,也可以换成一个回调函数。
const str = '2018年结束了,2019年开始了,2020年就也不远了'
const rex = /\d+/g // 这里是定义匹配规则,匹配字符串里的1到多个数字
const str1 = str.replace(rex, '****')
console.log(str1) // 输出:"****年结束了,****年开始了,****年也不远了"
const str2 = str.replace(rex, function(item){
console.log(arguments)
// [Arguments] {
// '0': '2018',
// '1': 0,
// '2': '2018年结束了,2019年开始了,2020年就也
// 不远了'
// }
// [Arguments] {
// '0': '2019',
// '1': 9,
// '2': '2018年结束了,2019年开始了,2020年就也 不远了'
// }
// [Arguments] {
// '0': '2020',
// '1': 18,
// '2': '2018年结束了,2019年开始了,2020年就也 不远了'
// }
console.log(item)
// 2018
// 2019
// 2020
const arr = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
let temp = ''
item.split('').map(function(i){
temp += arr[i]
})
return temp
})
console.log(str2) // 贰零壹捌年结束了,贰零壹玖年开始了,贰零贰零年也不远了2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
这个回调函数更简单
const str = '2018年结束了,2019年开始了,2020年就也不远了'
const rex = /\d/g // 这里是定义匹配规则,匹配字符串里的1到多个数字
const arr = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
const str2 = str.replace(rex, function(item){
return arr[item]
})
console.log(str2) // 输出:贰零壹捌年结束了,贰零壹玖年开始了,贰零贰零年也不远了2
3
4
5
6
7
8
9.search()
在目标字符串中搜索与正则规则相匹配的字符,搜索到,则返回第一个匹配项在目标字符串当中的位置,没有搜索到则返回一个-1。
const str = '2018年结束了,2019年开始了,2020年就也不远了'
const reg = /\d+/i // 这里是定义匹配规则,匹配字符串里的1到多个数字
console.log(str.search(reg)) // 输出 0 这里搜索到的第一项是从位置0开始的2
3
10.toLowerCase(),toUpperCase()
toLowerCase把字母转换成小写,toUpperCase()则是把字母转换成大写。
const str1 = 'abcdefg'
const str2 = 'ABCDEFG'
console.log(str2.toLowerCase()) // 输出:'abcdefg'
console.log(str1.toUpperCase()) // 输出:'ABCDEFG'2
3
4
11.includes(), startsWith(), endsWith()
includes、startsWith、endsWith,es6的新增方法,includes 用来检测目标字符串对象是否包含某个字符,返回一个布尔值,startsWith用来检测当前字符是否是目标字符串的起始部分,相对的endwith是用来检测是否是目标字符串的结尾部分。
const str = 'Excuse me, how do I get to park road?'
console.log(str.includes('how')) // 输出:true
console.log(str.startsWith('Excuse')) // 输出: true
console.log(str.endsWith('?')) // 输出: true2
3
4
12.repeat()
返回一个新的字符串对象,新字符串等于重复了指定次数的原始字符串。接收一个参数,就是指定重复的次数。原字符串不变。
const str = 'http'
const str2 = str.repeat(3)
console.log(str) // 输出:'http'
console.log(str2) // 输出:'httphttphttp'2
3
4
①charAt(),返回指定索引处的字符。返回的字符是长度为 1 的字符串
②indexOf(),返回某个指定的字符串值在字符串中首次出现的位置
③split(),将字符串分割成字符串数组,并返回此数组
④substring(),提取字符串中介于两个指定下标之间的字符,其内容是从 start 处到 stop-1 处的所有字符,其长度为 stop 减 start。