var str ="HELLO WORLD"// charAt(index) index为索引值var result = str.charAt(1)
console.log(result)// E
二、indexOf
var str ="Hello world, welcome to the universe."// indexOf(searchvalue,start) start可选,规定在字符串中开始检索的位置var result = str.indexOf("welcome")
console.log(result)// 13
三、includes
var str ="Hello world, welcome to the universe."// includes(searchvalue, start) start可选,规定在字符串中开始检索的位置var result = str.includes("welcome")
console.log(result)// true
四、split
var str ="Hello world welcome to the universe"// split(separator,limit) limit可选,指定返回的数组的最大长度var result = str.split(" ")var result1 = str.split(" ",3)
console.log(result)// ["Hello", "world", "welcome", "to", "the", "universe"]
console.log(result1)// ["Hello", "world", "welcome"]
五、trim
var str =" Hello "var result = str.trim(" ")
console.log(result)// Hello
六、slice
var str ="Hello world"// slice(start, end) start开始截取的下标,如果为负数,则从尾部开始截取// end可选,要截取的结尾的下标var result = str.slice(1,5)
console.log(result)// ello
七、replace
var str ="wesdTMMvewasTMM"// 部分替换var str1 = str.replace("TMM","***")
console.log(str1)// wesd***vewasTMMvar str ="Mr Blue has a blue house and a blue car"// 全局替换,常用来做一些敏感词过滤// /blue/gi可以忽略大小写, /blue/g有大小写区分var str1 = str.replace(/blue/g,"red")
console.log(str1)// Mr Blue has a red house and a red car