如何在字符串数组中的任意位置找到一个短语?

How can I find a phrase anywhere in a String Array?

我需要查看字符串数组中是否出现任何短语,例如“duckbilled platypus”。

在我测试的情况下,该短语确实存在于字符串列表中,如下所示:

然而,当我查找该短语时,如下所示:

...找不到它。我从来没有通过下面代码中的“如果(找到)”挑战。

这是我用来尝试遍历一个文档的内容以查看是否在两个文档中都找到了任何短语(两个词或更多)的代码:

private void FindAndStorePhrasesFoundInBothDocs()
{
    string[] doc1StrArray;
    string[] doc2StrArray;
    slPhrasesFoundInBothDocs = new List<string>();
    slAllDoc1Words = new List<string>();
    int iCountOfWordsInDoc1 = 0;
    int iSearchStartIndex = 0;
    int iSearchEndIndex = 1;
    string sDoc1PhraseToSearchForInDoc2;
    string sFoundPhrase;
    bool found;
    int iLastWordIndexReached = iSearchEndIndex; 
    try
    {
        doc1StrArray = File.ReadAllLines(sDoc1Path, Encoding.UTF8);
        doc2StrArray = File.ReadAllLines(sDoc2Path, Encoding.UTF8);
        foreach (string line in doc1StrArray)
        {
            string[] subLines = line.Split();
            foreach (string whirred in subLines)
            {
                if (String.IsNullOrEmpty(whirred)) continue;
                slAllDoc1Words.Add(whirred);
            }
        }
        iCountOfWordsInDoc1 = slAllDoc1Words.Count();
        sDoc1PhraseToSearchForInDoc2 = slAllDoc1Words[iSearchStartIndex] + ' ' + slAllDoc1Words[iSearchEndIndex];
        while (iLastWordIndexReached < iCountOfWordsInDoc1 - 1)
        {
            sFoundPhrase = string.Empty;
            // Search for the phrase from doc1 in doc2;                     
            found = doc2StrArray.Contains(sDoc1PhraseToSearchForInDoc2);
            if (found)
            {
                sFoundPhrase = sDoc1PhraseToSearchForInDoc2;
                iSearchEndIndex++;
                sDoc1PhraseToSearchForInDoc2 = sDoc1PhraseToSearchForInDoc2 + ' ' + slAllDoc1Words[iSearchEndIndex];
            }
            else //if not found, inc vals of BOTH int args and, if sFoundPhrase not null, assign to sDoc1PhraseToSearchForInDoc2 again. 
            {
                iSearchStartIndex = iSearchEndIndex;
                iSearchEndIndex = iSearchStartIndex + 1;
                if (!string.IsNullOrWhiteSpace(sFoundPhrase)) // add the previous found phrase if there was one
                {
                    slPhrasesFoundInBothDocs.Add(sFoundPhrase);
                }
                sDoc1PhraseToSearchForInDoc2 = slAllDoc1Words[iSearchStartIndex] + ' ' + slAllDoc1Words[iSearchEndIndex];
            } // if/else
            iLastWordIndexReached = iSearchEndIndex;                    
        } // while
    } // try
    catch (Exception ex)
    {
        MessageBox.Show("FindAndStorePhrasesFoundInBothDocs(); iSearchStartIndex = " + iSearchStartIndex.ToString() + "iSearchEndIndex = " + iSearchEndIndex.ToString() + " iLastWordIndexReached = " + iLastWordIndexReached.ToString() + " " + ex.Message);
    }
}

doc2StrArray 确实 包含搜索的短语,那么为什么 doc2StrArray.Contains(sDoc1PhraseToSearchForInDoc2) 失败了?

这应该可以满足您的要求:

found = Array.FindAll(doc2StrArray, s => s.Contains(sDoc1PhraseToSearchForInDoc2));

List<T> 中,Contains() 寻找 T,在您的代码中 foundtrue 必须具有特定索引中的所有文本(不是它)。 试试这个

var _list = doc2StrArray.ToList();
var found = _list.FirstOrDefault( w => w.Contains( sDoc1PhraseToSearchForInDoc2 ) ) != null;