我想使用c#检查字符串值是否包含字符串数组中的单词。例如,

string stringToCheck = "text1text2text3";

string[] stringArray = { "text1", "someothertext", etc... };

if(stringToCheck.contains stringArray) //one of the items?
{

}

我如何检查字符串值'stringToCheck'是否包含数组中的一个词?


当前回答

⚠️ Note: this does not answer the question asked
The question asked is "how can I check if a sentence contains any word from a list of words?"
This answer checks if a list of words contains one particular word

我会使用LINQ,但它仍然可以通过:

new[] {"text1", "text2", "etc"}.Contains(ItemToFind);

其他回答

Try:

String[] val = { "helloword1", "orange", "grape", "pear" };
String sep = "";
string stringToCheck = "word1";

bool match = String.Join(sep,val).Contains(stringToCheck);
bool anothermatch = val.Any(s => s.Contains(stringToCheck));

我使用下面的代码来检查字符串是否包含字符串数组中的任何项:

foreach (string s in stringArray)
{
    if (s != "")
    {
        if (stringToCheck.Contains(s))
        {
            Text = "matched";
        }
    }
}

一个不需要任何LINQ的简单解决方案:

String.Join(",", array).Contains(Value + ",");

您也可以尝试这个解决方案。

string[] nonSupportedExt = { ".3gp", ".avi", ".opus", ".wma", ".wav", ".m4a", ".ac3", ".aac", ".aiff" };
        
bool valid = Array.Exists(nonSupportedExt,E => E == ".Aac".ToLower());

也许是这样的:

string stringToCheck = "text1text2text3";
string[] stringArray = new string[] { "text1" };
if (Array.Exists<string>(stringArray, (Predicate<string>)delegate(string s) { 
    return stringToCheck.IndexOf(s, StringComparison.OrdinalIgnoreCase) > -1; })) {
    Console.WriteLine("Found!");
}