JS 字符串常用方法

字符串常用方法

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
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值