• 字符串的常用方法-增删改查、转换方法split


    1、增

    这里增的意思并不是说直接增添内容,而是创建字符串的一个副本,再进行操作

    除了常用+以及${}进行字符串拼接之外,还可通过concat

    concat
    用于将一个或多个字符串拼接成一个新字符串

    let stringValue = "hello ";
    let result = stringValue.concat("world");
    console.log(result); // "hello world"
    console.log(stringValue); // "hello"
    
    • 1
    • 2
    • 3
    • 4

    2、删

    这里的删的意思并不是说删除原字符串的内容,而是创建字符串的一个副本,再进行操作

    常见的有:

    • slice()
    • substr()
    • substring()
      这三个方法都返回调用它们的字符串的一个子字符串,而且都接收一或两个参数。
    let stringValue = "hello world";
    console.log(stringValue.slice(3)); // "lo world"
    console.log(stringValue.substring(3)); // "lo world"
    console.log(stringValue.substr(3)); // "lo world"
    console.log(stringValue.slice(3, 7)); // "lo w"
    console.log(stringValue.substring(3,7)); // "lo w"
    console.log(stringValue.substr(3, 7)); // "lo worl"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    3、改

    这里改的意思也不是改变原字符串,而是创建字符串的一个副本,再进行操作

    常见的有:

    • trim()、trimLeft()、trimRight()
    • repeat()
    • toLowerCase()、 toUpperCase()

    trim()、trimLeft()、trimRight()
    删除前、后或前后所有空格符,再返回新的字符串

    let stringValue = " hello world ";
    let trimmedStringValue = stringValue.trim();
    console.log(stringValue); // " hello world "
    console.log(trimmedStringValue); // "hello world"
    
    • 1
    • 2
    • 3
    • 4

    repeat()
    接收一个整数参数,表示要将字符串复制多少次,然后返回拼接所有副本后的结果

    let stringValue = "na ";
    let copyResult = stringValue.repeat(2) // na na 
    
    • 1
    • 2

    toLowerCase()、 toUpperCase()
    大小写转化

    let stringValue = "hello world";
    console.log(stringValue.toUpperCase()); // "HELLO WORLD"
    console.log(stringValue.toLowerCase()); // "hello world"
    
    • 1
    • 2
    • 3

    4、查

    除了通过索引的方式获取字符串的值,还可通过:

    • chatAt()
    • indexOf()
    • startWith()
    • includes()

    charAt()
    返回给定索引位置的字符,由传给方法的整数参数指定

    let message = "abcde";
    console.log(message.charAt(2)); // "c"
    
    • 1
    • 2

    indexOf()
    从字符串开头去搜索传入的字符串,并返回位置(如果没找到,则返回 -1 )

    let stringValue = "hello world";
    console.log(stringValue.indexOf("o")); // 4
    
    • 1
    • 2

    startWith()、includes()
    从字符串中搜索传入的字符串,并返回一个表示是否包含的布尔值

    let message = "foobarbaz";
    console.log(message.startsWith("foo")); // true
    console.log(message.startsWith("bar")); // false
    console.log(message.includes("bar")); // true
    console.log(message.includes("qux")); // false
    
    • 1
    • 2
    • 3
    • 4
    • 5

    5、转换方法:split

    把字符串按照指定的分割符,拆分成数组中的每一项

    let str = "12+23+34"
    let arr = str.split("+") // [12,23,34]
    
    • 1
    • 2
  • 相关阅读:
    删库不易,跑路更难
    Android学习笔记 3. EditText
    LeetCode 2530. 执行 K 次操作后的最大分数:优先队列(贪心)
    xml的语法
    论文解读:Example-Based Named Entity Recognition
    微服务保护Sentinel(二)-- 热点参数限流、隔离降级
    抖 X-Bongus 参数逆向 python案例实战
    每日10题
    Mapping the NFT revolution: market trends, trade networks, and visual features
    CF696B Puzzles 题解
  • 原文地址:https://blog.csdn.net/m0_66983971/article/details/126287410