如何在JavaScript中检查字符串是否包含子字符串?
栏目: JavaScript · 发布时间: 7年前
内容简介:这里有一个可用方法列表:性能测试表明,如果速度很重要,indexOf可能是最好的选择。:
这里有一个可用方法列表:
- (ES6) includes
var string = "foo",
substring = "oo";
string.includes(substring);
- ES5 and older indexOf
var string = "foo",
substring = "oo";
string.indexOf(substring) !== -1;
String.prototype.indexOf returns the position of the string in the other string. If not found, it will return -1.
- search
var string = "foo",
expr = /oo/;
string.search(expr);
- lodash includes
var string = "foo",
substring = "oo";
_.includes(string, substring);
- RegExp
var string = "foo",
expr = /oo/; // no quotes here
expr.test(string);
- Match
var string = "foo",
expr = /oo/;
string.match(expr);
性能测试表明,如果速度很重要,indexOf可能是最好的选择。
转载请注明出处:
http://zgljl2012.com/ru-he-zai-javascriptzhong-jian-cha-zi-fu-chuan-shi-fou-bao-han-zi-zi-fu-chuan/
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 查找一个字符串中最长不含重复字符的子字符串,计算该最长子字符串的长度
- 字符串、字符处理总结
- 高频算法面试题(字符串)leetcode 387. 字符串中的第一个唯一字符
- php删除字符串最后一个字符
- (三)C语言之字符串与字符串函数
- 算法笔记字符串处理问题H:编排字符串(2064)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Programming Python
Mark Lutz / O'Reilly Media / 2006-8-30 / USD 59.99
Already the industry standard for Python users, "Programming Python" from O'Reilly just got even better. This third edition has been updated to reflect current best practices and the abundance of chan......一起来看看 《Programming Python》 这本书的介绍吧!