通常我希望有一种String.contains()
方法,但是似乎没有。
有什么合理的方法来检查?
ECMAScript 6引入了String.prototype.includes
:
const string = "foo";
const substring = "oo";
console.log(string.includes(substring));
includes
但是不支持Internet Explorer。在ECMAScript 5或更旧的环境中,使用String.prototype.indexOf
,当找不到子字符串时,它将返回-1:
var string = "foo";
var substring = "oo";
console.log(string.indexOf(substring) !== -1);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句