Fork me on GitHub

ES6学习笔记之字符串的扩展

字符串的for of

ES6 为字符串添加了遍历器接口,使得字符串可以被for...of循环遍历。

const str='abcd';
for(let s of str){
     console.log(s)
}

✨模板字符串

//es5
var name='小明',
age=18;
console.log('我叫'+name+'今年'+age+'岁')

//es6
console.log(`我叫${name}今年${age}岁`);

注意:模板字符串使用 ``,即不是双引号"",也不是单引号''

``通常在键盘左上角ESC按键下方


新增字符串方法

String.includes(searchStr)

  • 描述:判断是否含有目标字符串searchStr
  • {
        const str='hello world';
        console.log(str.includes('hello'));//true
    }
    

String.startsWith(searchStr,position?)

  • 描述:判断字符串是否以searchStr开头,第二个参数position(可选)默认为0,表示从第几个字符开始向后判断
  • {
        const str='hello world';
        console.log(str.startsWith('hello'));//true
        console.log(str.startsWith('abc'));//false
        console.log(str,startsWith('hello',1));//false
        console.log(str,startsWith('world',6));//true
    }
    

String.endsWith(searchStr,position?)

  • 描述:判断字符串是否以searchStr结尾,同样第二个参数position(可选)默认为原字符串长度,表示从第几个字符开始向前判断
  • {
        const str='hello world';
      console.log('endsWith', str.endsWith('world'));//true
      console.log('endsWith', str.endsWith('abc'));//false
      console.log('endsWith', str.endsWith('world',11));//true
      console.log('endsWith', str.endsWith('hello', 5));//true
      console.log('endsWith', str.endsWith('hello', 7));//false
      
    }
    

String.repeat(n)

  • 描述:repeat方法返回一个新字符串,表示将原字符串重复n次。
    let str='abcd';
    str=str.repeat(3);
    console.log(str);//abcdabcdabcd

ES2017 引入了字符串补全长度的功能。如果某个字符串不够指定长度,会在头部或尾部补全。

1.String.padStart(length,fillStr?)头部补全

2.String.padEnd(length,fillStr?) 尾部补全

  • length: 补全后的字符串长度
  • fillstr: 用于填充的字符串,默认为空格
    let str='5678'
    console.log(str.padStart(8,'abc'));//abca5678
    console.log(str.padEnd(8,'abc'));//5678abca

实例

//提示日期
    let str = '08-12'
    console.log(str.padStart(10, 'yyyy-MM-dd'));//yyyy-08-12

ES2019 对字符串实例新增了trimStart()和trimEnd()这两个方法。它们的行为与trim()一致,trimStart()消除字符串头部的空格,trimEnd()消除尾部的空格。它们返回的都是新字符串,不会修改原始字符串。

const s = '  abcd  ';
s.trim() //"abcd"
s.trimStart() //"abc  "
s.trimEnd() //"  abc"
posted @   粥里有勺糖  阅读(404)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示