日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

判斷一個字符串中是否包含另一個字符串

作者:馬優晨 更新時間: 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

欄目分類
最近更新