字符串常用的方法
length属性 //获取字符串的长度 空格也算
var a = "hello world";alert(a.length) //11
字符串拼接
var year=19;var message="The year is "message += yearconsole.log(message) //The year is 19
1. 增加 concat
var str="hello world"str=str.concat("good");console.log(str) //hello worldgood
2. 查询
2.1.indexOf 查询字符串值的下标 如果没有返回-1
var str="hello world"var b = str.indexOf("h");console.loc(b) //0console.log(str.indexOf('g')) //-1
2.2slice(startIndex,end) 从哪一个下标开始,到某一个元素结束
字符串有空格的时候,空格也算一个字符
var str="hello world"console.log(str.slice(1,9)) //ello wor
2.3.substring(startIndex,end) 和slice作用一样
2.4.substr(index,howmany) 从下标值开始,截取多少位
var str="hello world"console.log(str.slice(0,3)) //helconsole.log(str.substring(0,3)) //helconsole.log(str.substr(0,4)) //hell
2.5.search 判断字符串是否存在某个值 存在则返回值的下标 空格也算,不存在则返回-1
2.6.startsWith 判断字符是不是以某个字符开头 返回true,false
var str="hello world"var http="https://www.baidu.com"console.log(str.search("w")) //6console.log(http.startsWith("https")) //true
3. 替换 replace()
var str="hello"console.log(str.replace("l","g")) //leglo
4. 分割成数组
4.1.split 可以将字符串分割为数组
var str="hello"var arr=str.split("")var ar=str.split("e")console.log(arr) //["h", "e", "l", "l", "o"]console.log(ar) // ["h", "llo"]
5 返回数组
5.1.match 返回匹配的值,是一个数组
var str="hello"console.log(str.match("e"))
