1、str.lenght
// 返回字符串长度
let str = "中国万岁"
const length = str.length;
console.log("length",length) // length 4
2、str.indexOf(str)
/*
作用:返回检索文本在字符串首次出现的位置
参数:需要检索的文本
返回值:如果该检索的文本存在则返回索引,不存在返回-1
*/
let str = "中国万岁"
const res1 = str.indexOf("中")
const res2 = str.indexOf("日")
console.log("res1",res1) // res1 0
console.log("res2",res2) // res2 -1
3、str.lastIndexOf(str)
/*
作用:返回检索文本在字符串最后出现的出现的位置
参数:需要检索的文本
返回值:如果该检索的文本存在则返回索引,不存在返回-1
*/
let str = "中国万岁,中国万岁"
const res1 = str.lastIndexOf("中")
const res2 = str.lastIndexOf("日")
console.log("res1",res1) // res1 5
console.log("res2",res2) // res2 -1
4、str.split()
/*
作用:根据所传参数分割字符串,该方法不会改变原字符串
参数:分隔符
返回值:数组
*/
let str = "中国万岁"
const arr1 = str.split();
console.log("arr1",arr1) // arr1 [ '中国万岁' ]
const arr2 = str.split('');
console.log("arr2",arr2) // arr2 [ '中', '国', '万', '岁' ]
console.log("str",str) // str 中国万岁
5、str.includes(str)
/*
作用:判断字符串是否含有某个字符
参数:需要检索的字符
返回值:布尔值
*/
let str = "中国万岁"
const bool = str.includes("中")
console.log("bool",bool) // bool true
6、str.slice(start,end)
/*
作用:截取字符串,该方法不会修改原字符串
参数:
(1)当省略第二个参数时,从索引start开始,截取到最后
(2)当参数为两个时,表示从索引start开始,截取到索引end处,不包括end
(3)当参数为负数时,从字符串末尾开始截取,不包括end处,最后一项,可以当做-1
返回值:截取后的字符串
*/
let str = "中国万岁";
const newStr1 = str.slice(1)
console.log("newStr1",newStr1) // newStr1 国万岁
const newStr2 = str.slice(1,2)
console.log("newStr2",newStr2) // newStr2 国
const newStr3 = str.slice(-4,-2)
console.log("newStr3",newStr3) // newStr3 中国
7、str.substring(start,end)
// 功能类似slice,但是不支持负数索引
8、str.substr(start,len)
/*
作用:截取字符串,类似于str.slice(),区别是,第二个参数是截取的长度
参数:
(1)当省略第二个参数时,从索引start开始,截取到最后
(2)当参数为两个时,表示从索引start开始,截取len个长度
(3)当参数为负数时,第二个参数不能传,因为长度不能为负数,最后一项,可以当做-1
返回值:截取后的字符串
*/
let str = "中国万岁"
const newStr1 = str.substr(1)
console.log("newStr1",newStr1) // newStr1 国万岁
const newStr2 = str.substr(1,2)
console.log("newStr2",newStr2) // newStr2 国万
const newStr3 = str.substr(-1)
console.log("newStr3",newStr3) // newStr3 岁
9、str.replace(str,newStr)
/*
作用:替换字符串的内容,该方法不会改变原字符串
参数:第一个参数为需要替换的参数,第二个参数为替换的新内容,对大小写很敏感
返回值:替换后的字符串
*/
let str = "China is very important in the world";
const newStr1 = str.replace("very","very very")
console.log("newStr1",newStr1) // newStr China is very very important in the world
const newStr2 = str.replace(/IS/gi,"IS")
console.log("newStr2",newStr2) // newStr2 China IS very important in the world
10、str.toUpperCase() && str.toLocaleUpperCase()
/*
作用:将字符串变成大写的,不会改变原字符串
参数:不需要传递参数
返回值:修改后的字符串
*/
let str = "hello world!"
const newStr = str.toUpperCase()
console.log("newStr",newStr) // newStr HELLO WORLD!
const newStr1 = str.toLocaleUpperCase()
console.log("newStr1",newStr1) // newStr1 HELLO WORLD!
11、str.toLowerCase() && str.toLocaleLowerCase()
/*
作用:将字符串变成大写的,不会改变原字符串
参数:不需要传递参数
返回值:修改后的字符串
*/
let str = "HELLO WORLD!"
const newStr = str.toLowerCase()
console.log("newStr",newStr) // newStr hello world!
const newStr1 = str.toLocaleLowerCase()
console.log("newStr1",newStr1) // newStr1 hello world!
12、str.concat(str)
/*
作用:字符串拼接,不会改变原字符串
参数:需要拼接的字符串,可以传递多个
返回值:拼接后的字符串
*/
let str = "中国";
const newStr = str.concat("你好","中国","万岁")
console.log("newStr",newStr) // newStr 中国你好中国万岁
13、str.trim()
/*
作用:去掉字符串首尾空格,不会改变原字符串
参数:不需要参数
返回值:新字符串
*/
let str = " 哈哈 "
console.log("newStr",str.trim()) // newStr 哈哈
14、str.charAt(index)
/*
作用:返回字符串指定位置的字符
参数:索引
返回值:该索引位置的字符
*/
let str = "中国你好";
console.log("char",str.charAt(0)) // char 中