Home >Backend Development >C++ >How Can I Check if a String Contains Any or All Strings from an Array in C#?
Determine String Inclusion in Array of Strings Using C#
You have a need to verify whether a given string, denoted as "stringToCheck," contains a word from a collection of strings stored in an array called "stringArray." To accomplish this, you seek a C# implementation.
To achieve this string comparison, leverage the LINQ extension method called "Any()." It takes a lambda expression as an argument, which specifies a condition. In this instance, the condition is whether the "stringToCheck" contains a particular element of the "stringArray."
Here's the code snippet that embodies this functionality:
using System.Linq; if (stringArray.Any(stringToCheck.Contains))
This code initially checks whether at least one of the substrings from "stringArray" is present within "stringToCheck." If you demand that all substrings be included, simply replace "Any" with "All":
if (stringArray.All(stringToCheck.Contains))
The above is the detailed content of How Can I Check if a String Contains Any or All Strings from an Array in C#?. For more information, please follow other related articles on the PHP Chinese website!