logologo
  • WebUI
  • JavaScript
  • TypeScript
  • C++
  • ··
    • C
    • Rust
    • Python
    • Cangjie
    • SQL
    • Design
    概述
    常见的数据结构
    ecmascript相关
    enumerable
    Object 内部实现
    sameValue 算法
    unicode
    语法设计
    声明
    表达式
    模块
    语句
    严格模式
    类型系统
    Array
    typedArray
    Map
    Number
    Object
    Set
    String
    结构化
    基本的组织元素
    动态性
    并发性
    范式
    函数式
    函数的行为
    闭包
    函数
    从运算式语言到函数式语言
    语言的多范式

    Last Updated: 10/5/2025, 4:45:35 AM

    Previous pageSetNext page结构化

    #String

    图标说明
    - 💥 (sideEffect,该方法有副作用)
    - 🗝️ (support String key,该方法的键名支持`String`数据类型)
    - 🔑 (support Symbol key,该方法的键名支持`Symbol`数据类型)
    - ⛓️ (use prototype chain ,该方法使用了原型链条)
        - 遍历原型对象及祖先原型对象时,需要顺着原型链向上

    #Instance

    实例化

    const str1 = "abc"; // 原始值
    const str2 = `abc`; // 原始值
    const str3 = String("abc"); // 原始值
    const str4 = new String("abc"); // String类型的对象实例

    #Prototype

    以下方法来自 String.prototype

    字符位置相关

    ecmaapidescribenote
    1charAt(index):String返回字符串中指定位置的字符index不能为负数
    2022at(index):String返回字符串中指定位置的字符用于取代 charAt,index可以使用负数
    1charCodeAt(index):Number返回指定位置字符的Unicode编码如果索引超出字符串范围,则返回NaN
    2015codePointAt(index):Number返回指定位置字符的Unicode编码该方法支持UTF-16编码的字符,可以正确处理4字节的Unicode字符。
    1indexOf(searchValue, fromIndex):Number找到匹配值的第一个索引如果未找到,则返回-1
    1lastIndexOf(searchValue, fromIndex):Number找到匹配值的最后一个索引如果未找到,则返回-1
    2015startsWith(searchString,position):Boolean是否以子串开头
    2015endsWith(searchString,endPosition):Boolean是否以子串结尾

    字符(串)匹配

    ecmaapidescribenote
    1search(regexp):Number返回第一个匹配的索引找不到返回-1。
    regexp 的 g 标志对结果没有影响,以正则表达式的 lastIndex 为 0 进行
    1localeCompare(that, [locales], [options]):Number比较两个字符串,根据本地特定的顺序返回比较结果that(要比较的字符串),locales( locales字符串或字符串数组),options(选项对象)
    1match(regexp):Array||Null匹配的结果(不含捕获组)如果使用了全局( g)标志,则返回与完整正则表达式匹配的所有结果,但也不会返回捕获组
    2018matchAll(regexp):Iterator匹配的结果(含捕获组)regex必须设置全局(g)标志
    2015includes(searchString,position):Boolean是否包含子串(大小写敏感)searchString(要搜索的字符串),position(可选,搜索开始的位置,默认为0
    1substring(start,end):Stirng返回选择范围的子串范围[start,end)
    1slice(start,end):String提取一部分字符串范围[start,end)

    剪接、替换

    ecmaapidescribenote
    1concat(...strs):String返回连接后的新字符串
    6repeat(count):String返回重复后的新字符串
    1replace(search,subString):String替换匹配的第一项为子串若 search为正则表达式,则会调用 regexp.exec进行匹配(可以使用$&等特殊字符来引用匹配结果)
    2021replaceAll():String替换所有匹配项为子串如果是正则表达式,必须带有全局标志g
    5trim():String删除头尾两处的连续空白符
    2019trimStart():String删除头部的连续空白符
    2019trimEnd():String删除尾部的连续空白符
    2017padStart(targetLength, padString)填充头部让字符串达到指定长度
    2017padEnd(targetLength, padString)填充尾部让字符串达到指定长度
    1split(separator, limit):Array按分隔符将字符串拆分成数组separator(分隔符,可以是字符串或正则表达式),
    limit(可选,返回数组的最大长度)

    其他工具

    ecmaapidescribenote
    2015normalize(form):String字符串转换为规范形式form可选,"NFC"(默认)、"NFD"、"NFKC"或"NFKD
    2019isWellFormed():Boolean检查字符串是否是良好形成的检查字符串是否是良好形成的,即没有被截断的字符,该方法有助于检测和避免字符串中的潜在编码问题。
    非标准toWellFormed():String将字符串转换为良好形成的状态将字符串转换为良好形成的状态,即修复被截断的字符
    1toLowerCase():String将字符串所有字符转换为小写
    1toUpperCase():String将字符串所有字符转换为大写
    1toLocaleLowerCase(locales):StringtoLowerCase的本地化版本
    1toLocaleUpperCase(locales):StringtoUpperCase的本地化版本
    1valueOf():String返回字符串对象的值
    1toString():String返回字符串对象的字符串表示形式
    2015String.prototypeSymbol.iterator已部署了可迭代接口

    #Static

    ecmaapidescribenote
    3String.fromCharCode(num1, ..., numN):String按照给定的码元,返回字符串
    2015String.fromCodePoint(codePoint1, ..., codePointN):String按照给定的码点,返回字符串
    2015String.raw(callSite, ...substitutions):RawString返回模板字符串点原始字符串形式表达式会被替换,但转义符不做处理