美文网首页
startsWith()和endsWith() 的使用

startsWith()和endsWith() 的使用

作者: 顺其自然AAAAA | 来源:发表于2020-09-09 11:07 被阅读0次

定义和用法
startsWith() 方法用于检测字符串是否以指定的子字符串开始。
如果是以指定的子字符串开头返回 true,否则 false。
startsWith() 方法对大小写敏感。

var str = 'Hello world, welcome to guangdong.';
let flag1 = str.startsWith('Hello');
console.log(flag1); // true

注意:一定要是开头的字符

let flag2 = str.startsWith('world');
console.log(flag2); //false

开头的字母也是可以的

    let flag3 = str.startsWith('H');
    console.log(flag3); // true

即使是第一个单词,但不是第一个字母也不行,返回false

let flag4 = str.startsWith('e');
console.log(flag4)  // false

定义:endsWith() 方法用于测试字符串是否以指定的后缀结束。用法和startsWith相似

 var str2 = "Hello world, welcome to beijing";
     let flag6 = str2.endsWith('beijing');
     console.log(flag6); // true
     let flag7 = str2.endsWith('welcome');
     console.log(flag7); // false

相关文章

网友评论

      本文标题:startsWith()和endsWith() 的使用

      本文链接:https://www.haomeiwen.com/subject/trheektx.html