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

string stringToCheck = "text1text2text3";

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

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

}

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


当前回答

试试这个。没有任何循环的需要。

string stringToCheck = "text1";
List<string> stringList = new List<string>() { "text1", "someothertext", "etc.." };
if (stringList.Exists(o => stringToCheck.Contains(o)))
{

}

其他回答

如果stringArray包含大量不同长度的字符串,可以考虑使用Trie存储和搜索字符串数组。

public static class Extensions
{
    public static bool ContainsAny(this string stringToCheck, IEnumerable<string> stringArray)
    {
        Trie trie = new Trie(stringArray);
        for (int i = 0; i < stringToCheck.Length; ++i)
        {
            if (trie.MatchesPrefix(stringToCheck.Substring(i)))
            {
                return true;
            }
        }

        return false;
    }
}

下面是Trie类的实现

public class Trie
{
    public Trie(IEnumerable<string> words)
    {
        Root = new Node { Letter = '\0' };
        foreach (string word in words)
        {
            this.Insert(word);
        }
    }

    public bool MatchesPrefix(string sentence)
    {
        if (sentence == null)
        {
            return false;
        }

        Node current = Root;
        foreach (char letter in sentence)
        {
            if (current.Links.ContainsKey(letter))
            {
                current = current.Links[letter];
                if (current.IsWord)
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }

        return false;
    }

    private void Insert(string word)
    {
        if (word == null)
        {
            throw new ArgumentNullException();
        }

        Node current = Root;
        foreach (char letter in word)
        {
            if (current.Links.ContainsKey(letter))
            {
                current = current.Links[letter];
            }
            else
            {
                Node newNode = new Node { Letter = letter };
                current.Links.Add(letter, newNode);
                current = newNode;
            }
        }

        current.IsWord = true;
    }

    private class Node
    {
        public char Letter;
        public SortedList<char, Node> Links = new SortedList<char, Node>();
        public bool IsWord;
    }

    private Node Root;
}

如果stringArray中的所有字符串都具有相同的长度,那么使用HashSet而不是Trie会更好

public static bool ContainsAny(this string stringToCheck, IEnumerable<string> stringArray)
{
    int stringLength = stringArray.First().Length;
    HashSet<string> stringSet = new HashSet<string>(stringArray);
    for (int i = 0; i < stringToCheck.Length - stringLength; ++i)
    {
        if (stringSet.Contains(stringToCheck.Substring(i, stringLength)))
        {
            return true;
        }
    }

    return false;
}

使用LINQ和方法组将是最快和更紧凑的方法。

var arrayA = new[] {"element1", "element2"};
var arrayB = new[] {"element2", "element3"};

if (arrayB.Any(arrayA.Contains)) 
    return true;
⚠️ 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

if (Array.IndexOf(array, Value) >= 0)
{
    //Your stuff goes here
}

我使用了类似于Maitrey684的IndexOf方法和Theomax的foreach循环来创建它。(注意:前3个“字符串”行只是一个例子,你可以创建一个数组,并把它变成适当的格式)。

如果您想比较两个数组,它们将以分号分隔,但最后一个值后面不会有分号。如果你在数组的字符串形式中添加了一个分号(即a;b;c变成了a;b;c;),你可以使用"x;"进行匹配,无论它在什么位置:

bool found = false;
string someString = "a-b-c";
string[] arrString = someString.Split('-');
string myStringArray = arrString.ToString() + ";";

foreach (string s in otherArray)
{
    if (myStringArray.IndexOf(s + ";") != -1) {
       found = true;
       break;
    }
}

if (found == true) { 
    // ....
}

方法如下:

using System.Linq;

if(stringArray.Any(stringToCheck.Contains))

/* or a bit longer: (stringArray.Any(s => stringToCheck.Contains(s))) */

这个函数检查stringToCheck是否包含stringArray的子字符串。如果你想确保它包含所有的子字符串,将Any更改为all:

if(stringArray.All(stringToCheck.Contains))