字符串

54 阅读4分钟

创建字符串

  1. 字面量方式创建
const str = "hello"
  1. 构造函数创建
const str = new String("hrllo")

字符串常用方法(不改变原字符串)

  1. charAt
    变量/字符串.charAt(下标)
    作用:根据下标的其字符串中寻找对应的值
    返回值:如果字符串中有对应的下标,那么返回对应的值,如果没有返回一个空字符串
const str = "YYJ"
const res = str.charAt(str[0])
console.log(res)       // Y
  1. charCodeAt
    变量/字符串.charCodeAt(下标)
    作用:根据下标的其字符串中寻找对应的值
    返回值:如果字符串中有对应的下标,那么返回对应的值,如果没有返回一个空字符串
const str = "YYJ"
const res = str.charAt(0)
console.log(res)       // 89
  1. toLowerCase
    变量/字符串.toLowerCase()
    作用:把字符串中所有的字母转换为小写
    返回值:转换后的字符串
const str = "YYJ"
const res = str.toLowerCase()
console.log(res)       // "yyj"
  1. toUpperCase
    变量/字符串.toUpperCase()
    作用:把字符串中所有的字母转换为大写
    返回值:转换后的字符串
const str = "yyj"
const res = str.toUpperCase()
console.log(res)       // "YYJ"
  1. substr
    变量/字符串.substr(开始下标, 多少个)
    作用:复制指定区域的字符串
    返回值:复制到的内容
const str = "YYJ"
const res = str.substr(0,1)
console.log(str)       // "YYJ" 
console.log(res)       // "Y"
  1. substring
    变量/字符串.substring(开始下标, 结束下标)
  • 包前不包后
  • 可以给负数,但是是倒着来的,不推荐写
  • 参数可以省略不写
    作用:复制指定区域的字符串
    返回值:复制到的内容
const str = "YYJ"
const res = str.substr(0, 1)
console.log(str)       // "YYJ" 
console.log(res)       // "Y"
  1. slice
    变量/字符串.slice(开始下标, 结束下标)
  • 包前不包后
  • 可以给负数
  • 参数可以省略不写
    作用:复制指定区域的字符串
    返回值:复制到的内容
const str = "qwertyuiop"
const res = str.slice(5, -1)
console.log(str)       // "qwertyuiop" 
console.log(res)       // "yuio"
  1. concat 变量/字符串.concat(数据1, 数据2, 数据3, 数据4, 数据5, ......) 作用:合并数据到执行的字符串中
    返回值:合并后的一个新字符串
const str = "YYJ"
const res = str.concat("@")
console.log(str)       // "YYJ@" 
console.log(res)       // "YYJ"
  1. indexOf
    变量/字符串.indexOf('要查询的数据', 开始查询的位置(可以不传, 默认为0))
    作用:去字符串中查询是否有这个数据
    返回值:如果找到返回对应下标,如果找不到返回-1
const str = "YYJ"
const res = str.indexOf("Y")
console.log(str)       // "YYJ"
console.log(res)       // 0
  1. lastIndexOf
    变量/字符串.indexOf('要查询的数据', 开始查询的位置(可以不传, 默认为0)) 作用:去字符串中查询是否有这个数据
    返回值:如果找到返回对应下标,如果找不到返回-1
const str = "YYJ"
const res = str.lastIndexOf("Y")
console.log(str)       // "YYJ"
console.log(res)       // 1
  1. split
    变量/字符串.split('分隔符')
    作用:根据我们传递的分隔符,将字符串做一个拆分
    返回值:拆分好的字符串,组成的一个数组
const str = "value=123=456=789"
const res = str.split("=")
console.log(str)        // "value=123=456=789"
console.log(res)        // ["value", "123", "456", "789"]
  1. trim
    变量.trim()
    作用:把字符串左右空格去除
    返回值:去除空格后的字符串
const str = " Y Y J "
const res = str.trim()
console.log(str)       // " Y Y J "
console.log(res)       // "YYJ"  
  1. trimStart(trimLeft)
    变量.trimStart()/变量.trimLeft()
    作用:把字符串左边空格去除
    返回值:去除左边空格后的字符串
const str = " Y Y J "
const res = str.trimStart()
console.log(str)       // " Y Y J "
console.log(res)       // "Y Y J "  
  1. trimEnd(trimRight)
    变量.trimEnd()/变量.trimRight()
    作用:把字符串右边空格去除
    返回值:去除右边空格后的字符串
const str = " Y Y J "
const res = str.trimEnd()
console.log(str)       // " Y Y J "
console.log(res)       // " Y Y J"  
  1. includes
    变量/字符串.includes(要查询的一些字符)
    作用:用于判断字符串中是否包含一段字符
    返回值:true/false
const str = "qwe123!@#$%^&*()asd"
const res = str.includes("qesa")
console.log(str)       // "qwe123!@#$%^&*()asd"
console.log(res)       // false 
  1. startWith
    变量/字符串.startsWith(要查询的一些字符)
    作用:用于判断字符串开头是否包含一段字符
    返回值:true/false
const str = "qwe123!@#$%^&*()asd"
const res = str.startWith("qwe123")
console.log(str)       // "qwe123!@#$%^&*()asd"
console.log(res)       // true 
  1. endWith
    变量/字符串.endsWith(要查询的一些字符)
    作用:用于判断字符串结尾是否包含一段字符
    返回值:true/false
const str = "qwe123!@#$%^&*()asd"
const res = str.startWith("asd")
console.log(str)       // "qwe123!@#$%^&*()asd"
console.log(res)       // true 
  1. replace
    变量/字符串.replace('查找的字符串', '要替换的内容')
    作用:去字符串中找到一个指定的内容,然后将新的内容替换过去
    返回值:true/false
const str = "qwe123!@#$%^&*()asd"
const res = str.startWith("asd", "****")
console.log(str)       // "qwe123!@#$%^&*()asd"
console.log(res)       // "qwe123!@#$%^&*()****"