• JS中字符串一些常用的方法


    以下是一些常用的 JavaScript 字符串操作方法,按照字母顺序进行分类:

    字符串操作方法

    • charAt(position): 返回指定位置的字符。
    • concat(string1, string2, …, stringN): 将两个或多个字符串连接起来。
    • includes(searchString, position):判断一个字符串是否包含另一个字符串。
    • endsWith(searchString, length):判断一个字符串是否以指定字符串结尾。
    • indexOf(searchValue, fromIndex):返回特定字符第一次出现的位置。
    • lastIndexOf(searchValue, fromIndex):返回特定字符最后一次出现的位置。
    • localeCompare(compareString, locales, options):比较两个字符串,并返回一个数字,表示比较结果。
    • match(regexp):找到一个或多个正则表达式的匹配。
    • repeat(count):将字符串重复指定的次数。
    • replace(searchValue, replaceValue):替换一个字符串中出现的指定字符。
    • search(regexp):检索字符串中指定的子字符串或正则表达式。
    • slice(startIndex, endIndex):提取字符串的一部分,并返回一个新的字符串。
    • split(separator, limit):将一个字符串拆分成子字符串数组。
    • startsWith(searchString, length):判断一个字符串是否以指定字符串开头。
    • substr(startIndex, length):提取字符串中指定位置的指定长度的子字符串。
    • substring(startIndex, endIndex):提取字符串中两个指定位置之间的字符。
    • toLocaleLowerCase():将字符串中的所有字符转换为小写字母。
    • toLocaleUpperCase():将字符串中的所有字符转换为大写字母。
    • toLowerCase():将字符串中的所有字符转换为小写字母。
    • toString():返回一个字符串。
    • toUpperCase():将字符串中的所有字符转换为大写字母。
    • trim():移除字符串两端的空白字符,并返回一个新的字符串。
    • valueOf():返回某个字符串对象的原始值。

    正则表达式操作方法

    • exec(string):在一个字符串中查找匹配,返回一个包含匹配的信息的数组。
    • test(string):检查一个字符串是否匹配某个模式,返回 true 或 false。
    • compile():将正则表达式编译为一个可重用的对象。

    使用方法:

    // 字符串操作方法示例
    const str = "Hello World";
    console.log(str.charAt(0)); // H
    console.log(str.concat("!!!")); // Hello World!!!
    console.log(str.includes("World")); // true
    console.log(str.endsWith("rld")); // true
    console.log(str.indexOf("World")); // 6
    console.log(str.lastIndexOf("l")); // 9
    console.log(str.localeCompare("hello world")); // 1
    console.log(str.match(/o/gi)); // [ 'o', 'o' ]
    console.log(str.repeat(3)); // Hello WorldHello WorldHello World
    console.log(str.replace("World", "JavaScript")); // Hello JavaScript
    console.log(str.search(/o/gi)); // 4
    console.log(str.slice(1, 4)); // ell
    console.log(str.split(" ")); // [ 'Hello', 'World' ]
    console.log(str.startsWith("Hello")); // true
    console.log(str.substr(1, 3)); // ell
    console.log(str.substring(1, 4)); // ell
    console.log(str.toLocaleLowerCase()); // hello world
    console.log(str.toLocaleUpperCase()); // HELLO WORLD
    console.log(str.toLowerCase()); // hello world
    console.log(str.toString()); // Hello World
    console.log(str.toUpperCase()); // HELLO WORLD
    console.log(str.trim()); // Hello World
    console.log(str.valueOf()); // Hello World
    
    // 正则表达式操作方法示例
    const regex = /\w+/;
    console.log(regex.exec(str)); // [ 'Hello', index: 0, input: 'Hello World', groups: undefined ]
    console.log(regex.test(str)); // true
    console.log(regex.compile(/\d+/)); // /\d+/
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
  • 相关阅读:
    html+css(1-50)
    16个值得推荐的.NET ORM框架
    叉乘分配律的几何证明
    Hadoop:YARN、MapReduce、Hive操作
    股市资讯天宇优配|政策利好叠加竞争格局向好 机构做多建材板块
    C语言适不适合新手学习?
    Apache Calcite入门
    2024年csdn最新最全的web自动化测试思路及实战
    SpringBoot2.6.x默认禁用循环依赖后的应对策略
    TikTok在美正式推出电商业务,变现路径直接改变!
  • 原文地址:https://blog.csdn.net/Jet_Lover/article/details/130838230