美文网首页重学es6
字符串 讲解之2 扩展方法

字符串 讲解之2 扩展方法

作者: zhang463291046 | 来源:发表于2020-08-18 15:53 被阅读0次

    以下内容是引用或者借鉴别人的,自己只是做个笔记,方便学习。理解错误的地方,欢迎评论。如有侵权,私聊我删除,未经允许,不准作为商业用途

    静态方法

    小知识说明:8bit(位)=1Byte(字节),1024Byte(字节)=1KB...所以16位需要2个字节存储(计算机中16进制一个值需要占用四个存储位,最大值F,比如1111 1111 = F F = 1字节),关于这方面的知识详细解说点击它。在不同操作系统中,一个字占用字节数也有所不同,如下表
    系统 字节
    16位的系统(8086微机) 1字 (word) 2字节(byte) 16(bit)
    32位的系统(win32) 1字 (word) 4字节(byte) 32(bit)
    64位的系统(win64) 1字 (word) 8字节(byte) 64(bit)
    • fromCodePoint()支持识别大于0xFFFF的码点
    String.fromCodePoint(0x20BB7)
    // "𠮷"
    
    //es5中,fromCharCode方法不法得到正确的值,因为不能识别大于0xFFFF的码点,所以0x20BB7就发生了溢出,最高位2被舍弃了,最后返回码点U+0BB7对应的字符
    String.fromCharCode(0x20BB7)
    // "ஷ"
    
    • raw该方法返回一个斜杠都被转义(即斜杠前面再加一个斜杠)的字符串,往往用模板字符串的处理方法
    String.raw(callSite, ...substitutions)
    String.raw`templateString`
    
    String.raw`foo${1 + 2}ba\r`  // 实际返回"foo3ba\\r",显示的是转义后的结果 "foo3ba\r"
    // 等同于
    String.raw({ raw: ['foo', 'bar'] }, 1 + 2)   // "foo3bar"
    

    实例方法

    • codePointAt() 返回一个字符十进制的码点
    // 汉字“𠮷”的码点是0x20BB7,UTF-16 编码为0xD842 0xDFB7(十进制为55362 57271)
    let s = '𠮷';
    s.length // 2 因为占用2个字节长度
    s.codePointAt() // 134071  参数默认是0
    s.codePointAt(0) // 134071
    s.codePointAt(1) // 57271
    s.codePointAt().toString(16) //20BB7
    
    s.charAt(0) // ''
    s.charAt(1) // ''
    s.charCodeAt(0) // 55362
    s.charCodeAt(1) // 57271
    
    • normalize()用来将字符的不同表示方法统一为同样的形式,这称为 Unicode 正规化
    //直接表示Ǒ(\u01D1)
    //合成表示 O(\u004F)和ˇ(\u030C)合成Ǒ(\u004F\u030C)
    '\u01D1'==='\u004F\u030C' //false
    '\u01D1'.length // 1
    '\u004F\u030C'.length // 2
    
    '\u01D1'.normalize() === '\u004F\u030C'.normalize()  // true
    
    • includes()返回布尔值,表示是否找到了参数字符串
    • startsWith()返回布尔值,表示参数字符串是否在原字符串的头部
    • endsWith()返回布尔值,表示参数字符串是否在原字符串的尾部
    let s = 'Hello world!';
    s.startsWith('Hello') // true
    s.endsWith('!') // true
    s.includes('o') // true
    
    // 第二个参数,表示从第n个字符直到字符串结束,endsWith表示字符串开头到第n个字符
    let s = 'Hello world!';
    s.startsWith('world', 6) // true
    s.endsWith('Hello', 5) // true
    s.includes('Hello', 6) // false
    
    • repeat()方法返回一个新字符串,表示将原字符串重复n
    // 参数是表示重复次数
    'x'.repeat(3) // "xxx"
    'hello'.repeat(2) // "hellohello"
    'na'.repeat(0) // ""
    
    • padStart()用于头部补全
    • padEnd()用于尾部补全
    // 第一个参数是字符串补全生效的最大长度,第二个参数是用来补全的字符串
    'x'.padStart(5, 'ab') // 'ababx'
    'x'.padStart(4, 'ab') // 'abax'
    
    'x'.padEnd(5, 'ab') // 'xabab'
    'x'.padEnd(4, 'ab') // 'xaba'
    
    • trimStart()消除字符串头部的空格
    • trimEnd()消除尾部的空格
    const s = '  abc  ';
    
    s.trim() // "abc"
    s.trimStart() // "abc  "
    s.trimEnd() // "  abc"
    
    • matchAll()方法返回一个正则表达式在当前字符串的所有匹配,在ES2020返回的是一个遍历器(Iterator),而不是数组
    const string = 'test1test2test3';
    const regex = /t(e)(st(\d?))/g;
    
    for (const match of string.matchAll(regex)) {
      console.log(match);
    }
    // ["test1", "e", "st1", "1", index: 0, input: "test1test2test3"]
    // ["test2", "e", "st2", "2", index: 5, input: "test1test2test3"]
    // ["test3", "e", "st3", "3", index: 10, input: "test1test2test3"]
    

    相关文章

      网友评论

        本文标题:字符串 讲解之2 扩展方法

        本文链接:https://www.haomeiwen.com/subject/ngspdktx.html