I need to check if any of a set of strings exists in one main string.
我需要检查一个主字符串中是否存在任何一组字符串。
Currently I'm using an array with a for loop to check each string.
目前我正在使用带有for循环的数组来检查每个字符串。
Is there a simpler way, like this solution for ruby which I've come across on - How to compare a string against multiple other strings
有没有更简单的方法,比如我遇到的ruby解决方案 - 如何将字符串与多个其他字符串进行比较
["string1","string2","string3"].include? myString
2 个解决方案
#1
3
How complex is your for loop? This is what loops exist for. Assuming your set of strings are an array called setOfStrings
and your main string is in a variable called mainString
:
你的for循环有多复杂?这就是循环的存在。假设您的字符串集是一个名为setOfStrings的数组,并且您的主字符串位于名为mainString的变量中:
var stringExists = false;
var setOfStrings = ['string1', 'string2', 'string3'];
for(var i =0;i < setOfStrings.length; i++){
if(mainString.indexOf(setOfStrings[i]) != -1)
stringExists = true;
}
It might not be as terse as your Ruby construct, but it's pretty darned common.
它可能不像你的Ruby构造那样简洁,但是它很常见。
#2
2
A did a little experiment and found that the following worked fine. The only overhead is the generation of the regexps for each element of the array. If check
is zero or above, one of the strings was found.
做了一个小实验,发现以下工作正常。唯一的开销是为数组的每个元素生成regexp。如果检查为零或以上,则找到其中一个字符串。
if (!('included' in Array.prototype)) {
Array.prototype.included = function() {
return arr.map(function (item) {
var regex = new RegExp(item, 'g');
if (regex.test(str)) return true;
return false;
}).indexOf(true);
};
}
To return a boolean instead of a number just change it to:
要返回布尔值而不是数字,只需将其更改为:
if (!('included' in Array.prototype)) {
Array.prototype.included = function () {
var regex, found = false;
for (var i = 0, l = this.length; i < l; i++) {
regex = new RegExp(this[i], 'g');
if (regex.test(str)) { found = true; break; }
}
return found;
};
}
var found = arr.included(str);
This is an edit of my previous answer because I realised I hadn't actually answered the question :)
这是我以前的答案的编辑,因为我意识到我实际上没有回答这个问题:)
#1
3
How complex is your for loop? This is what loops exist for. Assuming your set of strings are an array called setOfStrings
and your main string is in a variable called mainString
:
你的for循环有多复杂?这就是循环的存在。假设您的字符串集是一个名为setOfStrings的数组,并且您的主字符串位于名为mainString的变量中:
var stringExists = false;
var setOfStrings = ['string1', 'string2', 'string3'];
for(var i =0;i < setOfStrings.length; i++){
if(mainString.indexOf(setOfStrings[i]) != -1)
stringExists = true;
}
It might not be as terse as your Ruby construct, but it's pretty darned common.
它可能不像你的Ruby构造那样简洁,但是它很常见。
#2
2
A did a little experiment and found that the following worked fine. The only overhead is the generation of the regexps for each element of the array. If check
is zero or above, one of the strings was found.
做了一个小实验,发现以下工作正常。唯一的开销是为数组的每个元素生成regexp。如果检查为零或以上,则找到其中一个字符串。
if (!('included' in Array.prototype)) {
Array.prototype.included = function() {
return arr.map(function (item) {
var regex = new RegExp(item, 'g');
if (regex.test(str)) return true;
return false;
}).indexOf(true);
};
}
To return a boolean instead of a number just change it to:
要返回布尔值而不是数字,只需将其更改为:
if (!('included' in Array.prototype)) {
Array.prototype.included = function () {
var regex, found = false;
for (var i = 0, l = this.length; i < l; i++) {
regex = new RegExp(this[i], 'g');
if (regex.test(str)) { found = true; break; }
}
return found;
};
}
var found = arr.included(str);
This is an edit of my previous answer because I realised I hadn't actually answered the question :)
这是我以前的答案的编辑,因为我意识到我实际上没有回答这个问题:)