What would be the fastest way to check if a string contains any matches in a string array in C#? I can do it using a loop, but I think that would be too slow.
-
3Why do you think it would be too slow? Have you tested it? What is the typical size of your data set? Guessing like that is a waste of time. – Ed S. Nov 16 '10 at 04:02
-
Can you clarify the inputs and desired result? – Matthew Flaschen Nov 16 '10 at 04:05
-
It contains about 60 items, but in the same event I have more code. Performance is OK, but I was just wondering if I could have optimized this. – dnclem Nov 16 '10 at 04:08
-
See http://stackoverflow.com/questions/2930953/automatically-generating-regex-from-set-of-strings-residing-in-db-c/2931790#2931790 – Oren Trutner Nov 16 '10 at 04:08
-
3"The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet." - Michael A. Jackson (http://en.wikipedia.org/wiki/Program_optimization#Quotes) – Joe White Nov 16 '10 at 04:11
5 Answers
Using LINQ:
return array.Any(s => s.Equals(myString))
Granted, you might want to take culture and case into account, but that's the general idea. Also, if equality is not what you meant by "matches", you can always you the function you need to use for "match".

- 29,284
- 24
- 107
- 141
I really couldn't tell you if this is absolutely the fastest way, but one of the ways I have commonly done this is:
This will check if the string contains any of the strings from the array:
string[] myStrings = { "a", "b", "c" };
string checkThis = "abc";
if (myStrings.Any(checkThis.Contains))
{
MessageBox.Show("checkThis contains a string from string array myStrings.");
}
To check if the string contains all the strings (elements) of the array, simply change myStrings.Any
in the if statement to myStrings.All
.
I don't know what kind of application this is, but I often need to use:
if (myStrings.Any(checkThis.ToLowerInvariant().Contains))
So if you are checking to see user input, it won't matter, whether the user enters the string in CAPITAL letters, this could easily be reversed using ToLowerInvariant().
Hope this helped!

- 314
- 4
- 12
-
That's nice but what if you wanted to perform the check in the opposite direction... i.e. check whether that substring exists in the items of the array – Dave Lawrence May 15 '14 at 07:41
-
Many thanks, simple and seems to work and at my level is working reasonably fast. Not need nothing fancy :) – Juano Jun 03 '20 at 04:33
That works fine for me:
string[] characters = new string[] { ".", ",", "'" };
bool contains = characters.Any(c => word.Contains(c));
-
1Out of many solutions, This is the only one that worked inside a `Where` statement in ASP.NET Core. – Vahid Amiri May 09 '16 at 17:55
You could combine the strings with regex or statements, and then "do it in one pass," but technically the regex would still performing a loop internally. Ultimately, looping is necessary.

- 29,277
- 40
- 133
- 234
-
@david, even regex needs to loop, I think in most case it is still faster than doing so manually. Furthermore, it helps to cleaner code and thus easier to maintain, say, you need to change the matches criteria. – xandy Nov 16 '10 at 04:08
-
7Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems. – Barracoder Jan 09 '12 at 17:41
If the "array" will never change (or change only infrequently), and you'll have many input strings that you're testing against it, then you could build a HashSet<string>
from the array. HashSet<T>.Contains
is an O(1) operation, as opposed to a loop which is O(N).
But it would take some (small) amount of time to build the HashSet. If the array will change frequently, then a loop is the only realistic way to do it.

- 94,807
- 60
- 220
- 330