在另一个字符串中查找一个字符串/单词

时间:2020-10-07 18:37:36

标签: c# contains

我发现了很多与此问题类似的帖子,但似乎找不到适合我的帖子。

我要检查string str = "blablatestblabla"是否包含string str = "test"。 当完全匹配时,我可以使用它。否则,它将失败。 我已经尝试过几种方法,例如;

Regex.IsMatch()
str.Contains()
IndexOf()

此代码的目的是查找特定的字符串(完全匹配或字符串中的匹配项),将此字符串添加到列表中,然后继续添加下面的字符串,直到找到第二个搜索字符串为止。

希望您能理解我的问题。

谢谢!

编辑: 看来很多人都无法理解我的要求,所以我会尽力使事情变得更清楚。

假设我有一个包含以下内容的输入List<string>

fail
fail
start //Exact match (search string 1)
data 1 
data 2
end //Exact match (search string 2)
fail 
fail 

fail 
fail 
junkstartjunk //Contains "start" within the string (search string 1)
data 3
data 4
junkendjunk //Contains "end" within the string (search string 2)
fail 
fail 

无论是完全匹配还是包含匹配项(垃圾开始垃圾),我都将无法找到搜索字符串1(开始)。

我希望输出列表看起来像这样;

start
data 1
data 2
end

junkstartjunk
data 3
data 4
junkendjunk

到目前为止,这是我的代码:

public void FindAndRearrangeMsg(Converter converter)
    {
        List<string> txtInput = new List<string>();
        txtInput = converter.TxtInput; // Gets the user input.
        string strFindAndArrMsg1 = converter.StrFindAndArrMsg1; // Gets the first string to search for
        string strFindAndArrMsg2 = converter.StrFindAndArrMsg2; // Gets the second string to search for
        bool msg1Found = false; // Becomes TRUE if user's first string is found
        string newLine = "\n";

        foreach (string str in txtInput)
        {
            bool firstMsg = strFindAndArrMsg1.Contains(str); // User's first specific word
            bool secondMsg = strFindAndArrMsg2.Contains(str); // User's second specific word

            if (((!string.IsNullOrEmpty(str)) || (!string.IsNullOrWhiteSpace(str))) && firstMsg && msg1Found == false)
            {
                msg1Found = true;
                outputListData.Add(str.ToString()); // Add the first string
            }
            // Continue to add string between the first and the second search string.
            else if (!secondMsg && msg1Found) 
            {
                outputListData.Add(str.ToString());
            }
            //If the second search string and the first search string is found.
            else if (secondMsg && msg1Found)
            {
                outputListData.Add(str.ToString() + newLine);
                msg1Found = false;
            }
            else
            {
                msg1Found = false;
                continue;
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

就像@Eatos一样,问题陈述正确无误。但是似乎可以通过单个foreach循环来做到这一点。如果您位于所需的零件内,则只需要一点状态即可跟踪问题。

我认为做为迭代器更容易。

尚不清楚是要输入字符串包含开始/结束序列还是要包含输入字符串的开始/结束序列。我已经完成了输入字符串包含开始/结束的地方。

我还假设您要包括终止的开始/结束行。如果那不是您想要的,应该轻而易举地进行修改。

    static string[] TestData = new string[]
        {
            "one","randomtwotext","three","four","five","six","seven"
        };

    static void Main(string[] _)
    {
        const string start = "two";
        const string end = "six";

        var subset = GetBetween(TestData, start, end);

        foreach (var str in subset)
            Console.WriteLine(str);
    }

    static IEnumerable<string> GetBetween(IEnumerable<string> source, string start, string end)
    {
        bool inSequence = false;

        foreach (var str in source)
        {
            if (inSequence)
            {
                yield return str;

                if (str.Contains(end))
                    break;
            }
            else if(str.Contains(start))
            {
                yield return str;
                inSequence = true;
            }
        }
    }

答案 1 :(得分:-1)

我相信我能理解。您要遍历一些字符串列表,并创建一个新列表,该列表是第一个列表的子集,仅包含索引大于或等于第一个列表中包含给定字符串的第一个字符串的那些条目,并保持重写值,直到您在第一个列表中遇到一个包含第二个给定字符串的字符串。对吧?

如果是这样,那么我将不使用foreach循环,它不适合该工作。改为使用2个while循环(如果您不打算使用任何LINQ或其他任何循环)。

伪代码:

 
int i = 0;
int j = inputList.length;

// Keep ignoring list elements until we either out of bounds or found a match
while(i < j && !strFindAndArrMsg1.Contains(inputList[i])) {
 i += 1;
}

// inputList[i] - 1st item in the list that contains strFindAndArrMsg1

while(i < j && !strFindAndArrMsg2.Contains(inputList[i])) {
// keep adding values to the second list...
 i += 1;
}
// ...until we are either out ou bounds or found a second match.
// inputList[i] - 1st item in the list that contains strFindAndArrMsg2

根据边缘情况(是否要包含/排除找到的第1/2个元素),您可能需要调整索引/在循环之外添加其他值。

相关问题