• String 字符串的使用方法 -- JavaScript


    相关文章

    参考文章@CUGGZ

    1. charAt(index) 返回传入索引的字符

    const  string = 'abcdefg'
    const result = string.charAt(0)
    console.log(result) // 'a'
    
    • 1
    • 2
    • 3

    2. charCodeAt(index) 返回传入索引字符的unicode编码

    const  string = 'abcdefg'
    const result = string.charCodeAt(0)
    console.log(result) // 'a' => 97
    
    • 1
    • 2
    • 3

    3. indexOf('想要查找字符串中的元素')

    1. 如果字符串中有 我们要找的元素, 则返回该元素在字符串中第一次匹配到的索引
    2. 如果没有, 则返回 -1

    indexOf(searchvalue,fromindex)

    • searchvalue:必需,规定需检索的字符串值;
    • fromindex:可选的整数参数,规定在字符串中开始检索的位置。它的合法取值是 0 到 string.length - 1。如省略该,则从字符串的首字符开始检索。
    const  string = 'abcdbefg'
    const result = string.lastIndexOf('b')
    console.log(result)// 1
    
    • 1
    • 2
    • 3

    4. lastIndexOf('想要查找字符串中的元素')

    1. 如果字符串中有 我们要找的元素, 则返回该元素在字符串中最后一次匹配到的索引
    2. 如果没有, 则返回 -1
    const  string = 'abcdbefg'
    const result = string.lastIndexOf('b')
    console.log(result)// 4
    
    • 1
    • 2
    • 3

    5. includes('查找 字符串中是否包含某一个元素')

    1. 返回值是 truefalse

    includes(searchvalue, start)

    • searchvalue:必需,规定需检索的字符串值;
    • start:可选,设置从那个位置开始查找,默认为 0。
    const  string = 'abcdbefg'
    const result = string.includes('q')
    console.log(result)// false
    
    • 1
    • 2
    • 3

    6. startsWith ('判断字符串是否以某一个元素开头','从指定位置开始查找')

    1. 返回值是 true 和 false
    const  string = 'abcdbefg'
    const result = string.startsWith('a')
    console.log(result)// true
    
    • 1
    • 2
    • 3

    7. endsWith('判断字符串是否以某一个元素结尾','从指定位置开始查找')

    1. 返回值是 true 和 false
    const  string = 'abcdbefg'
    const result = string.startsWith('q')
    console.log(result)// false
    
    • 1
    • 2
    • 3

    8. concat('想要拼接的字符串')

    1. 返回连接后的新字符串
    const  string = 'abcdbefg'
    const result = string.concat('123')
    console.log(result)// abcdbefg123
    
    • 1
    • 2
    • 3

    9. split('以某一个符号进行拆分')

    1. 传入一个空字符串, 将会拆分每一个元素
    2. 可以同时拆分多个分割符,使用正则表达式即可实现

    string.split(separator,limit)

    • separator:必需。字符串或正则表达式,从该参数指定的地方分割 string。
    • limit:可选。该参数可指定返回的数组的最大长度。如果设置了该参数,返回的子串不会多于这个参数指定的数组。如果没有设置该参数,整个字符串都会被分割,不考虑它的长度。
    const string = 'abc,def;g'
    const result = string.split(',')
    const result2 = string.split('')
    const result3 = string.split(/[,;]/)
    console.log(result) //["abc","def;g"]
    console.log(result2) // ["a","b","c",",","d","e","f",";","g"]
    console.log(result3) //  ["abc","def","g"]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    10. slice('开始截取','截止到')

    1. 不传参数的话,默认截取所有字符。
    2. 该方法返回的子串包括开始处的字符,但不包括结束处的字符
    let  string = 'abcdefg'
    let result =string.slice(0,4)
    console.log(result) // 'abcd'
    
    • 1
    • 2
    • 3

    11. substr(start,length)

    let  string = 'abcdefg'
    let result =string.slice(0,1)
    console.log(result)// 'a'
    
    let string2 = 'abcdefg'
    let result2 =string2.substr(0,1)
    console.log(result2// 'a'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    12. substring(from,to)

    1. 该方法返回的子串包括开始处的字符,但不包括结束处的字符
    let  string = 'abcdefg'
    let result =string.slice(0,1)
    console.log(result) // a
    
    let string2 = 'abcdefg'
    let result2 =string2.substr(0,1)
    console.log(result2) //a
    
    let string3 = 'abcdefg'
    let result3 = string3.substring(0,1)
    console.log(result3) // a
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    13. toUpperCase() 字符串转大写

    let  string = 'abcdefg'
    let result = string.toUpperCase()
    console.log(result) //"ABCDEFG"
    
    • 1
    • 2
    • 3

    14. toLowerCase() 字符串转小写

    let string2 = 'ABCDEFG'
    let result2 = string2.toLowerCase()
    console.log(result2)
    
    • 1
    • 2
    • 3

    15. replace('匹配的字符串','替换匹配的字符串' ) 替换字符换

    1. 可以匹配单个字符,也可以匹配多个字符
    let string = 'abcdefg'
    let result = string.replace('c',6)
    console.log(result) // 'ab6defg'
    
    let str="Mr Blue has a blue house and a blue car";
    str.replace(/blue/gi, "red");    // 输出结果:'Mr red has a red house and a red car'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    16. match()

    1. 该方法返回存放匹配结果的数组
    2. 该方法用于在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。该方法类似 indexOf() 和 lastIndexOf(),但是它返回指定的值,而不是字符串的位置。
    let string = 'abcdefg'
    let result = string.match('c')
    console.log(result)//['c', index: 2, input: 'abcdefg', groups: undefined]
    
    • 1
    • 2
    • 3

    17. search()

    1. 返回 str 中第一个与 regexp 相匹配的子串的起始位置
    let string = 'abcdefg'
    let result = string.search('c')
    console.log(result)// 2
    
    • 1
    • 2
    • 3

    18. trim() 移除字符串首尾的空格

    let string = ' abcdefg '
    let result = string.trim()
    console.log(result)// 'abcdefg'
    
    • 1
    • 2
    • 3

    19. trimStart() 移除字符串开头的空格

    let string = ' abcdefg '
    let result = string.trimStart()
    console.log(result)// 'abcdefg '
    
    • 1
    • 2
    • 3

    20. trimEnd() 移除字符串结尾的空格

    let string = ' abcdefg '
    let result = string.trimStart()
    console.log(result)// ' abcdefg'
    
    • 1
    • 2
    • 3

    21. valueOf() 返回字符串本身

    let string = ' abcdefg '
    let result = string.valueOf()
    console.log(result)// 'abcdefg '
    
    • 1
    • 2
    • 3

    22. toString() 返回字符串本身

    let string = ' abcdefg '
    let result = string.toString()
    console.log(result)// 'abcdefg '
    
    • 1
    • 2
    • 3

    23. repeat() 重复字符串 n 次

    1. 如果传入的次数是小数 , 会向下取整
    2. 不传参数或者是传 0 ,都会返回一个空字符串

    24. padStart() 在字符串开头补足字符串

    1. 如果省略第二个参数, 则默认使用空格代替, 然后补足字符串的长度
    let string = ' cc '
    let result = string.padStart(8,'aa')
    console.log(result)// "aaaa cc "
    
    • 1
    • 2
    • 3

    padStart() 使用场景

    1. 将返回的页数补齐为三位,比如第1页就显示为001
    "1".padStart(3, '0')   // 输出结果: '001'
    "15".padStart(3, '0')  // 输出结果: '015'
    
    • 1
    • 2

    25. padEnd()

    let string = 'abcdefg'
    let result = string.padEnd(10,'6')
    console.log(result)// 'abcdefg666'
    
    • 1
    • 2
    • 3

    26. parseInt() 向下取整并将字符串转为数字类型

    let string = '10.665416'
    let result = parseInt(string)
    console.log(result)// 10
    
    • 1
    • 2
    • 3

    27. parseFloat() 返回一个浮点数

    let string2 = '10.665416'
    let result2 = parseFloat(string2)
    console.log(result2) // 10.665416
    
    let string3 = '10'
    let result3 = parseFloat(string3)
    console.log(result3) // 10
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    二、String 封装的相关方法

    1. 向一个字符串中插入自己想要的内容

    思路: - 利用 .slice() 方法 讲字符串拆分开,然后与自己想要插入的内容拼接起来.

    方法封装:
    //使用
    this.insertStr('202005',4,'/')
    
    ====================
    //方法:
    //soure 原字符串
    //start 位置
    //newStr 要插入的字符串
    insertStr(soure, start, newStr){
      return soure.slice(0, start) + newStr + soure.slice(start);
    },
    ====================
    //结果
    2020/05
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
  • 相关阅读:
    骨传导耳机品牌排名前十,盘点最受欢迎的五款TOP级骨传导耳机
    中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
    Kotlin协程分析(三)——理解协程上下文
    http协议与apache
    设计模式--代理模式
    SpringMVC入门
    NIO Selector选择器
    【iOS】—— 单例模式
    实战指南,SpringBoot + Mybatis 如何对接多数据源
    【勇敢饭饭,不怕刷题之链表】有序链表的合并
  • 原文地址:https://blog.csdn.net/i_Satan/article/details/126709933