我们很多时候会不自觉的在js代码里对一个字符串进行如下操作:

str.contains("substr");

 

但是js里面没有这个contains方法,这是后台的方法,而js提供了另一个方法indexOf:

 

var ceshi = str.indexOf("substr") 
if(ceshi != -1){
console.log("包含");
}
else if(ceshi == -1){
console.log("不包含");
}

 

 

如果值为-1,则不包含。

相关文章:

  • 2022-01-17
  • 2021-09-17
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2021-08-09
  • 2021-05-20
猜你喜欢
  • 2021-07-20
  • 2021-11-21
  • 2021-05-17
  • 2022-12-23
  • 2022-12-23
  • 2022-01-08
相关资源
相似解决方案