通常我会期望String.contains()
方法,但好像没有。
检查此问题的合理方法是什么?
推出 ECMAScript 6String.prototype.includes:
const string = "foo";
const substring = "oo";
console.log(string.includes(substring)); // true
String.prototype.includes
is 区分大小写 and is not由 Internet Explorer 支持没有polyfill.
在 ECMAScript 5 或更旧的环境中,使用String.prototype.indexOf,当找不到子字符串时返回 -1:
var string = "foo";
var substring = "oo";
console.log(string.indexOf(substring) !== -1); // true
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)