一,String
1,常见实例方法
(1) length:获取字符串长度
(2) split('分隔符'),用来将字符串拆分成数组
(3) substring(需要截取的第一个字符的索引 [,结束的索引号]),字符串截取
(4)startsWith(检测字符串[,检测位置索引号],检测是否以某字符开头
(5)includes(搜索的字符串[,检测位置索引号]) 判断一个字符串是否包含在另一个字符串中,根据情况返回True或者False
(6) toUpperCase 将字母转换成大写
(7) toLowerCase 将字母转换成小写
(8) indexOf 检测是否包含某字符
(9) endsWith 是否以某字符结尾
(10) replace 替换字符串,支持正则匹配
(11)match 查找字符串,支持正则匹配
2.split('分隔符')
//把字符串转换为数组
const str = 'red,green'
const arr = str.split(',')
//把数组转换为字符串
arr=[1,2,3]
const str = arr.join('/') //括号里边什么都没有的话默认是逗号
console.log(str)
3.substring(开始索引号[,结束索引号]) 截取出两个索引之间的子集
//没有结束索引号就默认截取到最后
const str = '是时间下课吃饭了'
console.log(str.substring(5,7)) //截取的不包含结束索引号的那个值
4.startsWith() 该方法用来判断当前字符串是否以另外一个给定的子字符串开头,并返回True或者False
str.startsWith(要搜索的子字符串[,位置]) :在str中搜索子字符串的开始位置,默认值为0(也就是从字符串的哪个位置开始搜索子字符串)如果在字符串开头找到了给定的字符则返回True,否则False
var str = 'To be, or not to be,that is the question.'; //To be逗号后面还有一个空格,所以这里算两个位置
alert(str.startsWith("To be")); //True
alert(str.startWith("not to be"); //False
alert(str.startWith("not to be",10)); //True
5.str.includes()里边参数跟startsWith()一样
与startsWith不同的是,includes里边只要含有该字符串即可,不论是否在开头。位置参数也一样,规定从哪个位置开始查找看字符串中是否含有该子字符串
const srt = 'To be, or not to be,that is the question.'
console.log(str.includes('To be')) //True
console.log(str.includes('not to be')) //True
console.log(str.includes('Not to be')) //False 因为js中严格区分大小写
console.log(str.includes('mother and father') //False
console.log(str.includes('to be',5) //False 从位置为5的地方开始查找是否含有子字符串