前端常用数据处理语法-1

复制代码
    // 一、charAt()方法 
    //     定义和用法
    // charAt() 方法可根据索引返回指定位置的字符 => 调用时记得return 需要返回值 没有值时默认索引为0
    let str = 'Hello,world!'
    console.log(str.charAt(1)) // e

    // 二、charCodeAt()方法
    //     定义和用法
    // charCodeAt() 方法可返回指定位置的字符的 => Unicode 编码 这个返回值是 0 - 65535 之间的整数
    let strCharCodeAt = 'Hello,world!'
    console.log(strCharCodeAt.charCodeAt(2)) // 108

    // 三、concat()方法
    //     定义和用法
    // concat()方法用于连接两个或多个数组
    // 该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本
    // 实例1、将参数连接到数组中
    let strConcat = [1, 2, 3, 4]
    console.log(strConcat.concat(3, 4)) // [1,2,3,4,3,4]
    // 实例2、两个数组连接起来
    let arrConcat = [1, 2, 3, 4]
    let arrConcat2 = [5, 6, 7, 8]
    console.log(arrConcat.concat(arrConcat2)) // [1, 2, 3, 4, 5, 6, 7, 8]

    // 实例2、三个数组连接起来
    let arrConcat3 = [9, 10, 11, 12]
    console.log(arrConcat.concat(arrConcat2, arrConcat3)) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

    // 四、fromCharCode()
    //     定义和用法
    // fromCharCode() 可接受一个指定的 Unicode 值,然后返回一个字符串
    console.log(String.fromCharCode(72, 69, 76, 76, 79).toLowerCase()) // hello

    // 五、indexOf()
    //     定义和用法 => 找不到返回-1
    // indexOf()方法可返回某个指定的字符串值在字符串中首次出现的位置的索引
    console.log(str.indexOf('o')) // 4 
    console.log(str.indexOf('Hello')) // 0
    console.log(str.indexOf(',')) // 5
    console.log(str.indexOf('world')) // 6

    // 六、lastIndexOf()
    //     定义和用法 => 找不到返回-1
    // lastIndexOf()方法可返回一个指定的字符串值最后出现的位置 在一个字符串中的指定位置从后向前搜索
    console.log(str.lastIndexOf('l')) // 9
    console.log(str.lastIndexOf(',')) // 5
    console.log(str.lastIndexOf('o')) // 7


    // 八、match()
    //     定义和用法 => 找不到返回null 正则中使用偏多
    // match() 方法可在字符串内检索指定的值 或找到一个或多个正则表达式的匹配
    console.log(str.match('Hello'))//['Hello',index:0,input:'Hello,wodld!',groups:undefined]
    console.log((str + 123).match(/\d+/g)) //['123] 匹配出所有的数字返回处理


    // 九、test()
    //     定义和用法 => 检索一个字符串是否匹配一个正则表达式 返回布尔值
    console.log(/\d+/g.test(str)) // false
    console.log(/\d+/g.test(str + '123')) // true

    // 十、replace(需要替换的值, 用来替换的值)
    //     定义和方法 => 替换字符
    console.log(str.replace(/world/, '独爱那杯cc')) // Hello,独爱那杯cc!
    // 将字符串中所有单词的首字母都转换为大写
    let name = 'aaa bbb ccc';
    uw = name.replace(/\b\w+\b/g, function (word) {
      return word.substring(0, 1).toUpperCase() + word.substring(1);
    }
    );
    console.log(uw)//'Aaa Bbb Ccc'

    // 十一、substring和substr截取字符串区别
    // 相同点:如果只是写一个参数,两者的作用都一样:都是是截取字符串从当前下标以后直到字符串最后的字符串片段。
    console.log(str.substring(1)) // ello,world!
    console.log(str.substr(1)) // ello,world!
    // 不同点:第二个参数
    console.log(str.substr(1, 9)) // ello,worl 第二个参数是截取这个字符串的长度
    console.log(str.substring(1, 9)) // ello,wor 第二个参数是截取字符串最终的下标

    // 十一、search()
    //       定义和用法 => 用于检索字符串中指定的子字符串 或检索与正则表达式相匹配的子字符串  返回下标 找不到是-1
    console.log(str.search(/Hello/)) // 0
    console.log(str.search(/hello/)) // -1  对大小写敏感
    console.log(str.search(/hello/i)) // 0  忽略大小写的检索
相关推荐
jearry2 分钟前
WebView2 原生拖放的桥接之道:拆解 yyzTools 的 drop-zone.js
前端·c++
2601_966949652 分钟前
批量 K 线不只是提速:因子研究中的数据质量、复权与回测偏差
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
matlab代码3 分钟前
基于matlab水果识别系统(西红柿、香蕉、梨、青椒)【源码64期】
开发语言·matlab
一位正在转型AI全栈的前端工程师3 分钟前
AI 全栈学习之旅 -Week 11:从 CLI 到浏览器:用 FastAPI、SSE 和 Vue 3 做一个可审核的 ReAct Agent
前端·python
王的宝库6 分钟前
Gin + GORM
开发语言·golang·gin
10share7 分钟前
我为什么用 React 重写了一个 VitePress
前端·react.js
零基础的修炼9 分钟前
CUDA知识汇总
开发语言·c++·编辑器
计算机魔术师9 分钟前
GPT-6 Astra 发布后,Sebastian Raschka 解析 looped transformer 与隐藏推理链传闻
前端
妙码生花12 分钟前
golang 应用服务端部署(使用 systemd 服务)
开发语言·人工智能·后端·golang·node.js·php·gin
不可能片场15 分钟前
Electron 退化成 node:一个环境变量的锅
前端·electron