• JavaScript字符串方法


    字符串

    ' string ',属原始值,不是对象只能直接调用属性和方法。

    通过JavaScript,原始值可以调用属性和方法,因为JavaScript在执行的时候,将原始值看作对象。

    1、字符串长度

    length属性(最常用,不做特别解释)

    2、查找字符串中的字符串

    1)indexOf():返回字符串中指定文本第一次出现的索引(从左往右计量,从0开始)。

       例:let str = 'this is a beatiful day!'

         let index = str.indexOf('is')  // 2

    2)lastIndexOf():返回字符串中指定文本最后一次出现的位置(从左往右计量,计算最后一次出现位置)。

       例:let str = 'this is a beatiful day!'

         let index = str.lastIndexOf('is') // 5

    注:若指定文本在字符串中并未出现,indexOf() 和 lastIndexOf() 都返回-1

      indexOf() 和 lastIndexOf() 都接受第二个参数--指定下标开始计量;

      例:let str = 'this is a beatiful day!'

           let index = str.indexOf('is', 3)  // 5

      例:let str = 'this is a beatiful day!'

           let index = str.lastIndexOf('is', 11) // 5

    重点总结: lastIndexOf() 传入第二个参数,会变成从右往左计量,在0 - 传入下标范围内,检索最后一次出现下标。相反,indexOf() 传入第二个参数,是在传入下标 - 最后一个字符的范围内,检索第一次出现下标。

    3)search():检索特定值字符串,并返回首次匹配位置(同 indexOf() );匹配文本不存在,返回-1。

       例:let str = 'this is a beatiful day!'

         let index = str.search('is') === let index = str.search(/is/) // 5 

    注:与indexOf() 区别

      -- search() 不支持第二个参数

      -- indexOf() 不支持正则匹配

    3、字符串截取

    1)slice(start,end):截取字符串的一部分,返回新字符串。

       start:必传,开始截取的下标,会截进去;可正负

       end:选传,结束截取的下标,不截进去;可正负

       -- 若开始下标为正,下标为0开始,从左往右计算,开始下标截进去,结束下标不截进去。

       例:let str = 'abcdefg hijklmn'

         let newStr = str.slice(2, 4) // cd

       -- 若开始下标为负,下标为1开始,从右往左计算,开始下标截进去,结束下标不截进去。

       例:let str = 'abcdefg hijklmn'

         let newStr = str.slice(-4, -2) // kl

       -- 若结束下标不传,默认从开始下标截取剩下字符串,开始下标截进去。

       例:let str = 'abcdefg hijklmn'

         let newStr = str.slice(2) // cdefg hijklmn

         let newStr = str.slice(-2) // mn

    2)substring(start,end):与slice() 类似,区别于substring() 不接受负数下标。

          start:必传,开始截取的下标,会截进去;只可为正

       end:选传,结束截取的下标,不截进去;只可为正;不传,默认截取剩下所有字符

       例:let str = 'abcdefg hijklmn'

         let newStr = str.substring(2, 4) // cd

    3)substr(start,length):与slice() 类似,区别于substr() 第二个参数是截取长度。

          start:必传,开始截取的下标,会截进去;可正负

       length:选传,截取长度;不传,默认截取剩下所有字符

       例:let str = 'abcdefg hijklmn'

         let newStr = str.substr(2, 4) // cdef

         let newStr1 = str.substr(2) // cdefg hijklmn

                 let newStr1 = str.substr(-2) // mn

    4、字符串替换

    replace():新值替换字符串中指定值。不会改变调用字符串,返回新字符串。

     例:let str = 'hello, this is a beatiful day!'

       let new1 = str.replace('beatiful', 'sad') // hello, this is a sadday!

    注:1)默认替换首次出现的指定值

         例:let new2 = str.replace('is', 'at') // hello, that is a beatiful day!

         若要替换所有出现的指定值,使用正则表达式(/g),全局搜索

         例:let new3 = str.replace(/is/g, 'at') // hello, that at a beatiful day!

      2)默认区分大小写,IS不会被匹配

         例:let new4 = str.replace('IS', 'at') // hello, this is a beatiful day!

         解决:使用正则表达式(/i),不区分大小写

         例:let new5 = str.replace(/IS/i, 'at') // hello, that is a beatiful day!

    5、转换大小写

    1)toUpperCase():将字符串中所有小写字符转为大写。

       例:let str = 'Hello World'

         let new1 = str.toUpperCase() // HELLO WORLD

    2)toLowerCase():将字符串中所有大写字符转为小写。

       例:let str = 'Hello World'

         let new2 = str.toLowerCase() // hello world

    6、字符串连接

    concat():连接两个或多个字符串。

     例:let str1 = '123'

       let str2 = '456'

       let str3 = '789'

    1)连接两个

       例:let new1 = str1.concat('连接符one', str2) // 123连接符one456

    2)连接多个

       例:let new1 = str1.concat('连接符one', str2, '连接符two', str3) // 123连接符one456连接符two789

    总结:用“ + ”号连接更方便。

    7、String.trim()  -- 了解一下

    删除字符串两端空白符。

    8、提取字符串

    1)charAt(index):传入下标,返回字符。

       例:let str = 'abcdefg'

         str.charAt(3) // d

    2)charCodeAt():传入下标,返回下标对应字符unicode编码。

       例:let str = 'abcdefg'

         str.charCodeAt(0) // 97

    注:大小写字符unicode编码不一样。

       例:let str = 'ABCDEFG'

         str.charCodeAt(0) // 65

    9、字符串转数组

    split():传入分隔符,将字符串转为数组。

     例:let str = 'a,b,c,d,e,f,g'

       1)传入分隔符,必须为字符串中存在的,否则无法分割字符串。

       let arr1 = str.split(',') // ['a', 'b', 'c', 'd', 'e', 'f', 'g']

       2)不传分隔符,字符串整体为一个元素,存在数组中。

       let arr2 = str.split() // ['a,b,c,d,e,f,g']

    10、属性访问(property access)--  拓展

    ES5(2009)允许对字符串的属性访问 [] 。

     例:let str = 'a,b,c,d,e,f,g'

       str[0] // a

    不太靠谱:

     1)不兼容IE7或更早版本;

     2)使字符串看起来像数组,其实并不是;

     3)如果找不到字符串,[] 会返回undefined;

     4)只读,不允许更改。str[0] = '1' 不会报错,也不会生效;

    注:如果需要用数组方式处理字符串,建议先将字符串转数组。

  • 相关阅读:
    小程序官方请求封装
    小程序天/小时/分秒倒计时封装
    小程序不定数量左右滑动中间放大轮播图效果
    小程序换行符检测换行
    小程序点击图片重新排序写法
    基于webuploader.js的单图片上传封装
    VMware Fusion 11 序列号
    Ionic 4 beta + Capacitor beta 尝鲜
    C语言学习笔记之动态分配数组空间
    C语言学习笔记之获取文件长度
  • 原文地址:https://www.cnblogs.com/candy-xia/p/13473752.html
Copyright © 2020-2023  润新知