C# 在字符串中查找完全匹配

发布于 2024-10-01 11:17:20 字数 158 浏览 1 评论 0原文

如何在字符串中搜索完全匹配的内容?例如,如果我有一个包含以下文本的字符串:

label
标签:
labels

我搜索标签,我只想得到第一个匹配项,而不是其他两个。我尝试了 Contains 和 IndexOf 方法,但它们也给了我第二个和第三个匹配项。

How can I search for an exact match in a string? For example, If I had a string with this text:

label
label:
labels

And I search for label, I only want to get the first match, not the other two. I tried the Contains and IndexOf method, but they also give me the 2nd and 3rd matches.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

世界和平 2024-10-08 11:17:20

您可以使用这样的正则表达式:

bool contains = Regex.IsMatch("Hello1 Hello2", @"(^|\s)Hello(\s|$)"); // yields false
bool contains = Regex.IsMatch("Hello1 Hello", @"(^|\s)Hello(\s|$)"); // yields true

\b 是单词边界检查,像上面那样使用它只能匹配整个单词。

我认为正则表达式版本应该比 Linq 更快。

参考

You can use a regular expression like this:

bool contains = Regex.IsMatch("Hello1 Hello2", @"(^|\s)Hello(\s|$)"); // yields false
bool contains = Regex.IsMatch("Hello1 Hello", @"(^|\s)Hello(\s|$)"); // yields true

The \b is a word boundary check, and used like above it will be able to match whole words only.

I think the regex version should be faster than Linq.

Reference

霊感 2024-10-08 11:17:20

您可以尝试拆分字符串(在这种情况下,右侧分隔符可以是空格,但这取决于情况),然后您可以使用 equals 方法查看是否存在匹配,例如:

private Boolean findString(String baseString,String strinfToFind, String separator)
{                
    foreach (String str in baseString.Split(separator.ToCharArray()))
    {
        if(str.Equals(strinfToFind))
        {
            return true;
        }
    }
    return false;
}

并且可以使用

findString("Label label Labels:", "label", " ");

You can try to split the string (in this case the right separator can be the space but it depends by the case) and after you can use the equals method to see if there's the match e.g.:

private Boolean findString(String baseString,String strinfToFind, String separator)
{                
    foreach (String str in baseString.Split(separator.ToCharArray()))
    {
        if(str.Equals(strinfToFind))
        {
            return true;
        }
    }
    return false;
}

And the use can be

findString("Label label Labels:", "label", " ");
为人所爱 2024-10-08 11:17:20

您可以尝试 LINQ 版本:

string str = "Hello1 Hello Hello2";
string another = "Hello";
string retVal = str.Split(" \n\r".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
                   .First( p => p .Equals(another));

You could try a LINQ version:

string str = "Hello1 Hello Hello2";
string another = "Hello";
string retVal = str.Split(" \n\r".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
                   .First( p => p .Equals(another));
帝王念 2024-10-08 11:17:20

单词之间似乎有一个分隔符 (crlf),因此您可以将该分隔符作为搜索字符串的一部分包含在内。

如果没有的话我会接受 Liviu 的建议。

It seems you've got a delimiter (crlf) between the words, so you could include the delimiter as part of the search string.

If not then I'd go with Liviu's suggestion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文