js:字符串的一些操作方法?

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script>
    // 字符串中常用方法

    // charAt 根据索引获取某个字符
    var str = 'hello helloworld'

    // 查找字符串中 索引2对应的那个字符
    // console.log(str.charAt(2))
    // console.log(str[2])

    // 区别 根据索引获取一个不存在的字符 返回值不同
    console.log(str.charAt(30)) // ""
    console.log(str[30]) // undefined

    // charCodeAt
    // 返回指定索引位置字符的Unicode编码
    var str = 'aA'
    console.log(str.charCodeAt(0)) // 'a' => 97
    console.log(str.charCodeAt(1)) // 'A' => 65

    // 根据Unicode编码 返回相应的字符
    String.fromCharCode(97) // 'a' 

    // concat 拼接字符
    // 'hello 2019'
    // var str = 'hello'
    // var result = str.concat(2019, ' ', 'byebye', 2018)
    // console.log(str)
    // console.log(result) // hello2019 byebye2018

    // console.log(str + 2019 + ' ' + 'byebye' + 2018) // "hello2019 byebye2018"

    // indexOf 查找指定字符 在字符串中的索引位置(从左到右查找)
    var str = "hello2019 byebye2018"
    console.log(str.indexOf('2')) // 5
    console.log(str.indexOf('2', 6)) // 16

    // lastIndexOf 查找指定字符 在字符串中的索引位置(从右到左查找)
    console.log(str.lastIndexOf('2')) // 16

    // replace() 替换字符中指定内容 适用于正则 (replace和正则搭配使用场景最多的)
    var str = 'hello worldoooo'

    // 默认情况下只能替换第一个字符匹配的字符
    // var result = str.replace('o', 'A')
    // console.log(str) // 'hello world'
    // console.log(result) // hellA world

    // global(全局)
    // var result = str.replace(/o/g, 'A')
    // console.log(result) // hellA wArldAAAA

    // var str = 'hello2019 byebye2018'
    // var result = str.replace(/\d+/g, '1111')
    // console.log(str) // 'hello2019 byebye2018'
    // console.log(result) // hello1111 byebye1111

    // slice substring substr
    var str = 'hello zhufeng'

    // 字符串的截取

    // slice(x) 从索引x处 开始截取到末尾
    // var result = str.slice(1) 
    // console.log(result) // ello zhufeng
    // console.log(str.slice(-2)) // 'ng'

    // slice(x, y) 从索引x处开始 截取到索引y处(不包含索引y这一项)
    // 包前不包后 [x, y)
    // console.log(str.slice(1, 3)) // 'el'

    // substring 跟slice一样 只是不支持负数作为索引
    var str = 'hello zhufeng'

    // substring(x) x代表截取起始索引
    console.log('81', str.substring(2)) // llo zhufeng

    console.log(str.substring(1, 3)) // el

    // substr
    // substr(x) 从索引x处开始截取到末尾 支持负数
    console.log('90', str.substr(2)) // 'llo zhufeng'
    console.log('90', str.substr(-2)) // 'ng'

    // substr(x, n) 从索引x处 开始截取n个字符
    console.log(str.substr(2, 2)) // 'll'

    // slice() substring() substr() 默认情况 根据原字符串完全克隆一份
    
    // split 将字符串转成数组
    // 根据指定分隔符 将字符串 拆分成数组
    var str = 'a,b,c,d,e'

    // 在默认情况下 没有指定分隔符 会把整个字符串 作为数组中第一项
    var result = str.split()
    console.log('103', result) // ["a,b,c,d,e"]

    // 将字符串按照‘,’ 作为分割符 将左右两边内容 作为数组中每一项
    console.log(str.split(',')) //  ["a", "b", "c", "d", "e"]

    // 如果指定一个不存在的字符 作为分隔符 按默认情况处理
    console.log(str.split('+')) // ["a,b,c,d,e"]

    // 传空字符 是把每一个字符 作为 数组中每一项
    str.split('') // ["a", ",", "b", ",", "c", ",", "d", ",", "e"]


    var arr = [1, 2, 3, 4]
    var res1 = arr.join('+')
    console.log(res1) // '1+2+3+4'

    console.log(res1.split('+')) // ["1", "2", "3", "4"]

    // split支持正则
    var str = 'a b c d'
    console.log(str.split(/\s/)) // ["a", "b", "c", "d"]

    // toUpperCase() 将字符串中所有字母转换为大写
    var str = 'hello 2019!'
    console.log(str.toUpperCase()) // 'HELLO 2019!'

    // toLowerCase() 将字符串中所有字母转换为小写
    var str = 'HELLO 2019!'
    console.log(str.toLowerCase()) // 'hello 2019!'

    // trim() 去除首尾空格
    var str = '  hello 2019!  '
    console.log(str.trim()) // "hello 2019!"












  </script>
</body>
</html>
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值