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