1-1 增加 concat()
<script>var str = "hello";var b = "world";var res = str.concat(b);console.log(res);</script>
1-2 查询
1-2-1 indexOf(value) 根据值查找对应的下标 找不到返回-1
<script>//可以查询某个字符的下标(下标从0开始)var str = "hello";console.log(str.indexOf("1")); -1表示没有</script>
1-2-2 search(value) 根据值查找对应的下标 找不到返回-1
<script>var str = "helloworld";var res = str.search("r");console.log(res); //7console.log(str.search("a")); //-1</script>
1-2-3 slice(startIndex,endIndex) 不包含最后一位
<script>var str = "hello";console.log(str.slice(1,4));</script>
1-2-4 includes 是否包含某位(多位)字符 返回boolean
<script>var test = "hello world";console.log(test.includes("world"));</script>
1-2-5 substr(index,length) 从哪里开始获取多少位
注意:第二个参数 为长度
<script>var str = "hellodakhlaui";console.log(str.substr(1,4)); //elloconsole.log(str.substr(0,4)); //hell</script>
1-2-6 substring(startIndex,endIndex) 不包含结束下标
开始下标
结束下标
<script>var str = "hello";var res = str.substring(1,4); //ellconsole.log(res);</script>
1-2-7 charAt(index) 根据下标查找对应的值
<script>var str = "hello";console.log(str.charAt(1)) //econsole.log(str[1]); //econsole.log(str.charAt(4)); //o</script>
1-2-8 match(value) 返回匹配的字符串,返回的是数组
存在返回数组
不存在返回null
<script>
//返回的是一个数组,将匹配的字符返回一个数组
var str = "你好你来你的"
var res = str.match("你");
console.log(res); //["你"]
console.log(str.match("它")); //null
</script>
1-3 length 字符串的长度
var str = "hello"
console.log(str.length);
var s = "故事的结尾,心上人"
console.log(handleStr(s));
function handleStr(value){
if(value.length>5){
return value.slice(0,5)+"..."
}
return value
}
1-4 其他
replace( ) 替换字符串
<script>
var str = "你好";
console.log(str.replace("你","它")); //它好
var s = "hello";
console.log(s.replace("l","*"));//he*lo
</script>
split( ) 将字符串分割成字符串数组
<script>
//将字符串分割为数组
var str = "hello";
console.log(str.split());
console.log(str.split(""));
console.log(str.split("e"));
</script>
1-4-3 trim( ) 去除字符串前后的空格
// 正则表达式: /^\s+|\s+$/g
var str = " hello ";
var arr = []
arr.push(str.trim())
console.log(arr)
1-4-4 startsWith( ) 以…开头的字符串
// startsWith() 以...开头的字符串
// endsWith()
var str = "武汉"
console.log(str.startsWith("武")) // true
console.log(str.endsWith("汉"))
