如果字符串等于c#中字符串中包含的单词,如何完全检查?

Pro*_*mer 2 c# string search

也许这个问题有点困惑.我将以更好的方式解释这个代码

string myfirststring = "hello1,hello123,content";
string_2 = "ent";
if (myfirststring.Contains(string_2)){
    Console.WriteLine("Yes! this is included in the 1 string");
    //then check if string_2 is equal to the 3 element of 1 string: content
}
Run Code Online (Sandbox Code Playgroud)

现在我想检查我的包含字符串"string_2"是否完全等于myfirststring的3个元素:content(在这种情况下不是因为值是ent而不是内容所以ent!= content)所以我该如何检查?

InB*_*een 5

如果单词以逗号分隔,则可以执行以下操作:

var exactlyContained = myFirstString.Split(',').Any(w => w == string_2);
Run Code Online (Sandbox Code Playgroud)

true如果在任何其他情况下找到完全匹配,则返回false.

UPDATE

根据评论,您的字符串似乎有不同符号序列分隔的单词.也许您可以使用这种自定义拆分方法(您可以根据您的特定需求进行调整,我没有太多信息):

public static IEnumerable<string> CustomSplit(this string s)
{
    var buffer = new StringBuilder();

    foreach (var c in s)
    {
        if (!char.IsLetterOrDigit(c))
        {
            if (buffer.Length > 0)
            {
                yield return buffer.ToString();
                buffer.Clear();
            }
        }
        else
        {
            buffer.Append(c);
        }
    }

    if (buffer.Length > 0)
        yield return buffer.ToString();
}
Run Code Online (Sandbox Code Playgroud)

现在代码将是:

var exactlyContained = myFirstString.CustomSplit().Any(w => w == string_2);
Run Code Online (Sandbox Code Playgroud)