学无先后,达者为师

网站首页 编程语言 正文

判断一个字符串中是否包含另一个字符串

作者:马优晨 更新时间: 2022-02-17 编程语言

第一种办法: ES6语法

contains()

判断一个字符串中是否包含某个字符串

startsWith()

判断一个字符串中是否以某个字符串开始

endsWith()

判断一个字符串中是否以某个字符串结尾

var s = "hello world!";

s.starsWith("hello");   //true
s.endWith("!");       //true
s.contains("o");       //true

第二种办法: 使用indexof

const str = "测试一个字符串111是否包含另外一个字符串";
    if (str.indexOf("111") >= 0) {
         alert('字符串中包含111字符串');
      } else {
         alert('字符串中 不包含111字符串');
    }

原文链接:https://mayouchen.blog.csdn.net/article/details/78889587

栏目分类
最近更新