04 字符串的新增方法

1.String.fromCodePoint()
2.String.raw()
3.String.codePointAt()
4.实例方法:includes(),startsWith(),endsWidth()
5.实例方法:repeat()
6.实例方法:padStart()、padEnd()
7.实例方法:trimStart()、trimEnd()

1.String.fromCodePoint()

String.fromCodePoint(0x20BB7)
// "𠮷"

用于从 Unicode 码点返回对应字符。

2.String.raw()

该方法返回一个斜杠都被转义(即斜杠前面再加一个斜杠)的字符串,往往用于模板字符串的处理方法。
console.log(String.raw`Hi\\n` === "Hi\\\\n")

3.String.codePointAt()

let s = '𠮷a';
console.log(s.codePointAt(0))  // 134071

4.实例方法:includes(),startsWith(),endsWidth()

之前只有一个indexOf()方法来确定一个字符串是否包含在另一个字符串中。
  • includes():返回布尔值,表示是否找到了参数字符串。
  • startsWith():返回布尔值,表示参数字符串是否在原字符串的头部。
  • endsWith():返回布尔值,表示参数字符串是否在原字符串的尾部。
let s = 'Hello world!'
console.log(s.startsWith('Hello')); // true
console.log(s.endsWith('!'));      // true
console.log(s.includes('o'));      // true

5.实例方法:repeat()方法

返回一个新字符串,表示将原字符串重复了n次。
console.log('x'.repeat(3)) // xxx

6.实例方法:padStart()、padEnd()

字符串长度补全功能,一共接收两个参数;
第一个是字符串补全生效的最大长度;
第二个是用来补全的字符串。

console.log('x'.padStart(5,'ab')) // ababx
console.log('x'.padStart(4,'ab'))  //abax

console.log('x'.padEnd(5,'ab'))  //xabab
console.log('x'.padEnd(4,'ab'))  //xaba

7.实例方法:trimStart()、trimEnd()

const s = '  abc  '
console.log(s.trim())
console.log(s.trimStart())
console.log(s.trimEnd())

之前消除字符串空格直接用trim,现在可以trimStart()消除头部空格或trimEnd()消除尾部空格。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值