前台之路——JavaScript中的String(全)

作者 : 开心源码 本文共3149个字,预计阅读时间需要8分钟 发布时间: 2022-05-12 共249人阅读

JavaSript中的String

字符串是JS中的基本数据类型之一,与数组有很多类似的地方,本文对字符串做一个总结

属性

  • length
    这里的length指的是字符串类型数据的属性,而不是构造函数String的length。
    • 构造函数的length属性是作为函数的通用属性,表明形参的个数;
    • 字符串类型数据的length字符串中字符编码单元的数量,对于使用两个代码单元表示的不常用字符,length的值与实际字符的个数不一致

方法

  • 静态方法

    • String.fromCharCode()
      返回由指定的UTF-16代码单元序列创立的字符串

        String.fromCharCode(189, 43, 190, 61)   // "?+?="
    • String.fromCodePoint()
      返回使用指定的代码点序列创立的字符串

        String.fromCharCode(9731, 9733, 9842, 0x2F804)    // "?★??"
  • 实例方法

    • charAt
      从一个字符串中返回指定的字符,参数是index,默认为0,未找到返回空字符串

        const s = '12345'  s.charAt()  // "1"  s.chatAt(2) // "3"  s.chatAt(-1)  // ""
    • concat

      将一个或者多个字符串与原字符串连接合并,形成一个新的字符串并返回

        const s = 'hello'  s.concat(' world', ' tom')  // "hello world tom"
    • endsWith

      用来判断当前字符串能否是以另外一个给定的子字符串“结尾”的,根据结果返回true或者者false。可以传入第二个参数,指定从右往左匹配的长度

        const s = 'hello world'  s.endsWith('d')     // true  s.endsWith('world', 7)  // true。相当于 'o world'.endsWith('world')
    • includes

      用于判断一个字符串能否包含在另一个字符串中,根据结果返回true或者者false。可以传入第二个参数,指定开始匹配的索引。

        const s = 'hello'  s.includes('h') // true  s.includes('h', 1)  //false
    • indexOf

      从左向右查找第一次出现指定值的索引,未找到则返回 -1。可指定查找起始索引。

        const s = 'hello world'  s.indexOf('o')  // 4
    • lastIndexOf

      从右向左查找第一次出现指定值的索引,未找到则返回 -1。可指定查找起始索引。

        const s = 'hello world'  s.lastIndexOf('o')  // 7
    • localeCompare

      比较参数字符串能否在目标字符串前面,返回正数、零和负数。可以传入locale,指定语言区域

        const s = 'hello'  s.localeCompare('world')    // 'h' 在 'w' 前面,返回正数
    • match

      参数为正则表达式,返回一个字符串匹配正则表达式的的结果。假如不指定参数,返回[""]

        var s = 'For more information, see Chapter 3.4.5.1'  var re = /see (chapter \d+(\.\d)*)/i  var found = s.match(re)    console.log(found)  // ["see Chapter 3.4.5.1", "Chapter 3.4.5.1", ".1"]  /** 该数组还有额外属性:    groups: undefined,     index: 22,     input: "For more information, see Chapter 3.4.5.1"  **/
    • matchAll

      match相似,区别是返回的结果是包含所有匹配结果的迭代器

        const regexp = RegExp('foo*','g')  const str = 'table football, foosball'  let matches = str.matchAll(regexp)    for (const match of matches) {    console.log(match)  }    // ["foo", index: 6, input: "table football, foosball", groups: undefined]  // ["foo", index: 16, input: "table football, foosball", groups: undefined]
    • normalize

      按照指定的一种 Unicode 正规形式将当前字符串正规化

    • padEnd

      用指定字符串从末尾开始填充任前字符串,返回填充后达到指定长度的字符串

        const s = 'hello'  s.padEnd(10) // "hello     "  s.padEnd(10, 'a')   // "helloaaaaa"  s.padEnd(10, 'ab')  // "helloababa"
    • padStart

      与padEnd相似,只不过是从起始位置开始填充

    • repeat

      构造并返回一个新字符串,该字符串包含被连接在一起的指定数量的字符串的副本。参数为指定的重复次数

      • 传入负数,Infinity都会报错
      • 传入NaN会返回空字符串
      • 传入小数会向下取整
        const s = 'hello'  s.repeat(-1)    // Uncaught RangeError: Invalid count value  s.repeat(Infinity)  // Uncaught RangeError: Invalid count value  s.repeat(NaN)   // ""  s.repeat(1.5)   // "hello"  s.repeat(2) // "hellohello"
    • replace

      返回一个替换后的新字符串,参数可以是

      • 字符串
      • 正则表达式
      • 回调函数
        const s = 'hello'  s.replace('e', 'a') // "hallo"
    • search

      返回找到的字符串第一次出现的下标

        const s = 'hello'  s.search('l')   // 2
    • slice

      提取字符串的一部分,返回新字符串。参数为起始位置和结束位置

        const s = 'hello'  s.slice(2, 5)   // "llo"
    • split

      按照指定分隔符将字符串切分成字符串数组

        const s = 'hello world'  s.split('') //['h','e','l','l','o','','w','o','r','l','d']  s.split(' ')    // ["hello", "world"]
    • startsWith

      相似endsWith,区别在于判断该字符串能否以指定字符串开头

    • substring

      返回指定下标之间的子字符串,与slice相同,区别在于

      • 假如起始下标大于结束下标,则substring会把两个下标交换位置,而slice会返回空字符串
      • 假如任何一个下标为负数,则substring会将其当作0来解决,而slice则是加上该字符串的长度
        const s = 'hello'  s.substring(1, -3)  // "h"  ==> s.substing(1, 0) ==> s.substring(0, 1)  s.slice(1, -3)  // "e"
    • toLowerCase

      将字符串转成小写

        const s = 'HEllo'  s.toLowerCase() // hello
    • toUpperCase

      将字符串转成大写

        const s = 'HEllo'  s.toUpperCase() // HELLO
    • trim

      从一个字符串的两端删除空白字符

        const s = '  hello  '  s.trim()    // hello
    • trimRight

      相似trim,从一个字符串的右边删除空白字符

    • trimLeft

      相似trim,从一个字符串的左边删除空白字符

    • valueOf

      返回一个对象的原始值

        const s = new String('hello world') // String {"hello"}  s.valueOf()     // "hello world"

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » 前台之路——JavaScript中的String(全)

发表回复