字符串常用方法
length 字符串长度
这个相对简单,不多赘述
const str = 'hello shadiao';
console.log(str.length); // 13
还有另一种方法,使用 … 扩展运算符
const str = 'hello shadiao';
console.log([...str].length); // 13
split 分割字符串为数组
split 方法将字符串分割为数组,参数为以XXX分割
const str = 'hello shadiao';
console.log(str.split(' ')); // [ 'hello', 'shadiao' ]
还可匹配正则,实现多个分割符分割字符串,以下例子实现以空格和逗号进行分割
const str = 'hello shadiao,my name is world';
console.log(str.split(/[ ,]/)); // [ 'hello', 'shadiao', 'my', 'name', 'is', 'world' ]
也可使用 … 扩展运算符进行简单的分割
const str = 'hello shadiao';
console.log([...str]); // ['h', 'e', 'l', 'l','o', ' ', 's', 'h','a', 'd', 'i', 'a','o']
repeat 复制字符串
repeat 方法将字符串进行复制,参数为复制的次数
const str = 'hello shadiao! ';
console.log(str.repeat(2)); // hello shadiao! hello shadiao!
反转字符串
组合扩展操作符 … 、Array.reverse方法和Array.join方法
const str = 'hello shadiao';
console.log([...str].reverse().join('')); // oaidahs olleh
组合split方法、Array.reverse方法和Array.join方法
const str = 'hello shadiao';
console.log(str.split('').reverse().join('')); // oaidahs olleh
截取字符串
slice()方法
slice() 方法截取字符串中特定位置,参数表示从start截取的end,不包含end。
const str = 'hello shadiao';
console.log(str.slice(1, 7)); // ello s
substr()方法
substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符(包含start)。第一个参数表示截取字符串的起始下标,为负数则表示尾部开始算起的位置;第二个参数表示字符串中的字符数,如果省略了该参数,那么返回从开始位置到结尾的字符串。
const str = 'hello shadiao';
console.log(str.substr(1, 7)); // ello sh
尾部开始截取
const str = 'hello shadiao';
console.log(str.substr(-1)); // o
console.log(str.substr(-9)); // o shadiao
substr() 可以替代 substring() 和 slice() 来使用
替换字符串中的特定序列
replace()
replace() 方法替换匹配的正则
只匹配第一个符合正则的序列
const str = 'hello shadiao';
console.log(str.replace(/shadiao/, 'world')); // hello world
const str = 'hello shadiao, i like shadiao';
console.log(str.replace(/shadiao/, 'world')); // hello world, i like shadiao
全局匹配正则替换 /xxx/g
const str = 'hello shadiao, i like shadiao';
console.log(str.replace(/shadiao/g, 'world')); // hello world, i like world
replaceAll()
replaceAll() 方法替换所有符合的序列(兼容性问题)
const str = 'hello shadiao, i like shadiao';
console.log(str.replaceAll('shadiao', 'world')); // hello world, i like world
填充字符串到指定的长度
padStart() 以特定字符串在开头补充至指定长度
padEnd() 以特定字符串在结尾补充至指定长度
const str = '001';
console.log(str.padStart(8, '0')); // 00000001
console.log(str.padEnd(8, '0')); // 00100000
字符串大小写转换
toUpperCase() 方法字符串转大写
const str = 'hello shadiao';
console.log(str.toUpperCase()); // HELLO SHADIAO
toLowerCase() 方法字符串转小写
const str = 'HELLO SHADIAO';
console.log(str.toLowerCase());
首字符转大写
const str = 'hello shadiao';
console.log(str[0].toUpperCase() + str.substr(1)); // Hello shadiao
或
const str = 'hello shadiao';
arr = [...str];
arr[0] = arr[0].toUpperCase();
console.log(arr.join('')); // Hello shadiao
检查字符串是否包含特定序列
includes() 方法检查字符串是否包含特定序列
const str = 'hello shadiao';
console.log(str.includes('shadiao')); // true
console.log(str.includes('world')); // false
检查字符串是否以特定序列开头或结尾
startsWith() 方法检查字符串是否以特定序列开头
endsWith() 方法检查字符串是否以特定序列结尾
const str = 'hello shadiao';
console.log(str.startsWith('hello')); // true
console.log(str.startsWith('shadiao')); // false
console.log(str.endsWith('hello')); // false
console.log(str.endsWith('shadiao')); // true