• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

JS : 操作 String 字符串

武飞扬头像
玄鱼殇
帮助1

学新通

目录

String - JavaScript | MDN

一. 访问字符串字符

二. 字符串遍历

1 -  普通循环

2 - for...of循环

三. 修改字符串

四. toUpperCase : 将所有字符变为大写

五. toLowerCase : 将所有字符变为小写

六. 查找字符串

1 - indexOf ( searchString , fromIndex ) : 查找

2 - includes( searchString , position ) : 查找

3 - startsWith : 以xxx开头

4 - endsWith : 以xxx结尾

七. 替换字符串

1 - replace : 替换找到的第一个

2 - replaceAll : 替换找到的所有

八. 获取子字符串 

九. 拼接字符串

1 -  

2 - concat

十. trim

1. trim : 删除字符串首尾的空格

2. trimStart : 删除字符串首的空格

3. trimEnd : 删除字符串尾的空格

十一. split( swparator,limit ) : 字符串分割 

十二. Object.entries( )遍历

十三. 字符串填充

1 - padStart : 左边填充

2 - padEnd : 右边填充

十四. 处理 URL 的查询字符串 URLSearchParams -  | MDN


String - JavaScript | MDN

一. 访问字符串字符

  1.  
    const message = 'hello world'
  2.  
     
  3.  
    console.log( message[1] ) // e
  4.  
    console.log( message.charAt(4) ) // o

二. 字符串遍历

1 -  普通循环

  1.  
    const message = 'hello world'
  2.  
     
  3.  
    for (let i = 0; i < message.length; i ) {
  4.  
    console.log(message[i])
  5.  
    }

2 - for...of循环

  1.  
    // for..of的遍历 -> 迭代器
  2.  
    // 目前可迭代对象: 字符串/数组
  3.  
    // 对象是不支持for..of
  4.  
    // String对象内部是将字符串变成了一个可迭代对象
  5.  
     
  6.  
    for (let char of message) {
  7.  
    console.log(char)
  8.  
    }

三. 修改字符串

当字符串定义后是不可以修改的,所以当直接操作字符是无效的

  1.  
    let message = "Hello World"
  2.  
     
  3.  
    message[2] = "A"
  4.  
    console.log(message) // Hello World. 不会更改的

四. toUpperCase : 将所有字符变为大写

  1.  
    const message = 'hello'
  2.  
     
  3.  
    console.log(message.toUpperCase()) // HELLO ,原来的字符串没有更改,而是生成了新的字符串

五. toLowerCase : 将所有字符变为小写

  1.  
    const message = 'HeLLo'
  2.  
     
  3.  
    console.log(message.toUpperCase()) // hello,原来的字符串没有更改,而是生成了新的字符串

六. 查找字符串

1 - indexOf ( searchString , fromIndex ) : 查找

  • 情况一: 搜索到, 搜索字符串所在索引位置
  • 情况二: 没有搜索到, 返回-1
  1.  
    const message = 'my name is coder'
  2.  
     
  3.  
    let index = message.indexOf('name')
  4.  
    if(index !== -1){
  5.  
    console.log('找到了')
  6.  
    }else{
  7.  
    console.log('没找到')
  8.  
    }

2 - includes( searchString , position ) : 查找

  1.  
    const message = 'my name is coder'
  2.  
     
  3.  
    if(message.includes('name')){
  4.  
    console.log('找到了')
  5.  
    }

3 - startsWith : 以xxx开头

  1.  
    const message = 'my name is coder'
  2.  
     
  3.  
    if (message.startsWith("my")) {
  4.  
    console.log("message以my开头")
  5.  
    }

4 - endsWith : 以xxx结尾

  1.  
    const message = 'my name is coder'
  2.  
     
  3.  
    if (message.endsWith("coder")) {
  4.  
    console.log("message以my结尾")
  5.  
    }

七. 替换字符串

1 - replace : 替换找到的第一个

  • 查找到对应的字符串,并且使用新的字符串进行替代
  • 也可以传入一个正则表达式来查找,也可以传入一个函数来替换
  1.  
    // 传入字符串
  2.  
    const message = 'my name is star'
  3.  
    let newMessage = message.replace("star", "kobe")
  4.  
    console.log(newMessage) // my name is kobe
  5.  
     
  6.  
    // 传入函数
  7.  
    const newName = "kobe"
  8.  
    let newMessage = message.replace("star", function() {
  9.  
    return newName.toUpperCase()
  10.  
    })
  11.  
    console.log(newMessage) // my name is KOBE

2 - replaceAll : 替换找到的所有

  1.  
    const info = 'my name is star, star star is good';
  2.  
    // 只替换找到的第一个
  3.  
    const onlyFirstOne = info.replace('star', 'coder');
  4.  
    console.log(onlyFirstOne); // my name is coder, star star is good
  5.  
     
  6.  
    // 全部替换
  7.  
    const allChange = info.replaceAll('star', 'coder');
  8.  
    console.log(allChange); // my name is coder, coder coder is good

八. 获取子字符串 

学新通

  1.  
    const message = "Hello World"
  2.  
     
  3.  
    console.log(message.slice(3, 7)) // lo W
  4.  
    console.log(message.slice(3, -1)) // lo Worl
  5.  
    console.log(message.slice(3)) // lo World
  6.  
     
  7.  
    console.log(message.substr(3, 7)) // lo Worl

九. 拼接字符串

1 -  

  1.  
    const str1 = "Hello"
  2.  
    const str2 = "World"
  3.  
    const str3 = "kobe"
  4.  
     
  5.  
    const newString = str1 str2 str3
  6.  
    console.log(newString)

2 - concat

  1.  
    const str1 = "Hello"
  2.  
    const str2 = "World"
  3.  
    const str3 = "kobe"
  4.  
     
  5.  
    // 可链式调用
  6.  
    const newString2 = str1.concat(str2).concat(str3)
  7.  
    // 可同时传入多个值
  8.  
    const newString3 = str1.concat(str2, str3, "abc", "cba")

十. trim

1. trim : 删除字符串首尾的空格

console.log("    star      abc   ".trim()) // star      abc

2. trimStart : 删除字符串首的空格

console.log("    star      abc   ".trimStart()) // star      abc___ 这里有空格的哈

3. trimEnd : 删除字符串尾的空格

console.log("    star      abc   ".trimEnd()) // ____star      abc

十一. split( swparator,limit ) : 字符串分割 

  • separator:以什么字符串进行分割,也可以是一个正则表达式;
  • limit:限制返回片段的数量;
  1.  
    const message = "abc-cba-nba-mba"
  2.  
    // 切割字符为 => -
  3.  
    const items = message.split("-")
  4.  
    console.log(items) // ['abc','cba','nba','mba']
  5.  
    // 通过数组的join方法,可变为字符串 连接字符为 *
  6.  
    const newMessage = items.join("*")
  7.  
    console.log(newMessage) // abc*cba*nba*mba
  8.  
     
  9.  
    //-----------------------------------------------
  10.  
     
  11.  
    const message = 'abc-cba-nba-mba';
  12.  
    // 返回长度为2的数组
  13.  
    const items = message.split('-', 2);
  14.  
    console.log(items); // ['abc','cba']

十二. Object.entries( )遍历

  1.  
    console.log(Object.entries('Hello')); // [['0', 'H'], ['1', 'e'], ['2', 'l'], ['3', 'l'], ['4', 'o']]
  2.  
     
  3.  
    const str = 'Hello';
  4.  
    for (const item of Object.entries('Hello')) {
  5.  
    const [index, value] = item;
  6.  
    console.log(index, value); // 0 H | 1 e | 2 l | 3 l | 4 o |
  7.  
    }

十三. 字符串填充

padStart |padEnd (length,str)

  • length : 该字符串的总长度
  • str : 用什么字符来填充

1 - padStart : 左边填充

  1.  
    // 此时字符串长度为3
  2.  
    let info = 'abc';
  3.  
    // 设定拼接结束字符串总长度为5,不足部分用u来填充
  4.  
    info = info.padStart(5, 'xx');
  5.  
     
  6.  
     
  7.  
    let str = 'address';
  8.  
    // 如果字符串本身的长度>=传入的参数,则不改变
  9.  
    str = str.padStart(5, 'xx');
  10.  
    console.log(str); // address

2 - padEnd : 右边填充

  1.  
    let cardNumber = '132666200001018899';
  2.  
    // 截取前四位
  3.  
    const sliceNumber = cardNumber.slice(0, 4);
  4.  
    // 把除前四位外的其他数字用 **** 来填充
  5.  
    cardNumber = sliceNumber.padEnd(cardNumber.length, '*');
  6.  
    console.log(cardNumber); // 1326**************

十四. 处理 URL 的查询字符串 URLSearchParams -  | MDN

可用来获取地址栏上的 ? 后携带的数据

  1.  
    // 从地址栏上拿到数据,可用split切割一下 ?
  2.  
    const searchString = '?name=star&age=18&height=1.88';
  3.  
    // 处理 URL 的查询字符串
  4.  
    const params = new URLSearchParams(searchString);
  5.  
    // 啥也看不出来
  6.  
    console.log(params);
  7.  
    // 可直接拿到
  8.  
    console.log(params.get('name')); // star
  9.  
    console.log(params.get('age')); // 18
  10.  
    // 也啥都看不出来
  11.  
    console.log(params.entries()); // Iterator {}
  12.  
     
  13.  
    // 但是可以遍历出来
  14.  
    for (const item of params.entries()) {
  15.  
    console.log(item); // ['name', 'star']、['age', '18']、['height', '1.88']
  16.  
    }
  17.  
    // 所以直接在数组中解构
  18.  
    console.log([...params.entries()]); // [ ['name', 'star'], ['age', '18'], ['height', '1.88'] ]
  19.  
     
  20.  
    // 转换成对象 也可以直接 Object.fromEntries(params)
  21.  
    const paramObj = Object.fromEntries(params.entries());
  22.  
    console.log(paramObj); // {name: 'star', age: '18', height: '1.88'}
  23.  
     
  24.  
    // --------------------一步搞定--------------------
  25.  
    const searchStringTwo = '?name=liuli&id=8&info=havemoney';
  26.  
    const obj = Object.fromEntries(new URLSearchParams(searchStringTwo));
  27.  
    console.log(obj); // {name: 'liuli', id: '8', info: 'havemoney'}
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhghacbg
系列文章
更多 icon
同类精品
更多 icon
继续加载