xinggevip

es2015字符串的查找和拼接
index.html<!DOCTYPE html> <html lang="en&qu...
扫描右侧二维码阅读全文
10
2019/06

es2015字符串的查找和拼接

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<ul id="demo"></ul>



<script>
    // 传统查找
    let str = "app banana orange";
    // 如果找到返回下标,如果找不到返回-1
    console.log(str.indexOf("xxx"));

    // es2015查找
    // 如果找到返回true,如果找不到返回false
    console.log(str.includes("app"));
    // 判断开头是否为指定字符串
    console.log(str.startsWith("app"));
    // 判断结束是否为指定字符串
    console.log(str.endsWith("app"));
    // 重复字符串
    console.log(str.repeat(10));
    // 在字符串开头增加字符串
    let str1 = "abc";
    let str2 = "def";
    console.log(str2.padStart(str1.length + str2.length,str1));
    // padends用法同上
  
</script>
</body>
</html>

运行结果

-1
true
true
false
app banana orangeapp banana orangeapp banana orangeapp banana orangeapp banana orangeapp banana orangeapp banana orangeapp banana orangeapp banana orangeapp banana orange
abcdef
Last modification:June 10th, 2019 at 10:23 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment