concat
<script>
var str = "hello";
var s = "world";
console.log(str.concat(" ").concat(s));//hello world
</script>
字符串模板
作用:可以在字符串中使用变量
<script>
var str = 10;
var b = "hello";
console.log(str+b);
var sum = `${str}hello`;
console.log(sum)
</script>
slice(startIndex,endIndex)
<script>
var str ="hello";
console.log(str[0]);//h
console.log(str.slice(0,3))//hel
</script>
substr(index,length) \substring(startIndex,endIndex)
<script>
var str = "hello";
console.log(str.substr(2,3));//llo
console.log(str.substring(0,2))//he
</script>
length 字符串的长度
<script>
var str = "hello";
console.log(str.length)//5
var s = "故事的结尾,心上人";
console.log(handleStr(s))//故事的结尾...
function handleStr(value) {
if (value.length > 5) {
return value.slice(0, 5) + "..."
}
return value;
}
</script>
charAt
输出某一位下标值的字符
<script>
var str = "hello";
console.log(str.charAt(1))//e
</script>
indexof
输出某一字符的下标值
<script>
var str ="hello";
console.log(str.indexOf("h"))//0
console.log(str.indexOf("h"))//-1
</script>
search
<script>
/* search 返回值的下标,没有返回-1,和indexOf类似 */
var str = "你是谁";
var index = str.search("你");
console.log(index)
console.log(str.indexOf("他"))
</script>
match
<script>
/* match返回匹配的字符,返回一个数组 */
var str = "helllllo";
var arr = str.match("l");
var arr1 = str.match("a");
console.log(arr[0]);//l
console.log(arr1);//null
</script>
includes
字符串是否包含某位(多位)字符
<script>
var str ="hello";
console.log(str.includes("he"))//true
</script>
replace
<script>
/* replace可以替换字符 */
var str = "hello";
console.log(str.replace("l","*"))
</script>
split
<script>
/* split(seprate) */
var str ="hello";
var arr = str.split("");
console.log(str.split())//["hello"]
console.log(arr);//["h", "e", "l", "l", "o"]
console.log(str.split("e"))//["h", "llo"]
</script>
例子(将hello反转为olleh)
<script>
var str = "hello"
var arr = str.split("");
arr.reverse();
var s = arr.join("")
console.log(s);
</script>
trim
<script>
httpList()
var a = " hello world "
var b = a.trim()
console.log(a.length)// hello world /
console.log(b.length)//hello world
console.log(a.length)//16
console.log(b.length)//11
</script>
字符串转成正则表达式
var str = "/\\" + left[i] + "/";
var reg = eval(str)