使用 LINQ 在字符串中查找单词

发布于 2024-11-06 14:09:17 字数 565 浏览 0 评论 0原文

欢迎使用 C# 或 VB.NET 提出建议。

我有以下代码:

 Dim someText = "Stack Over Flow Community"
    Dim someWord = "Over Community"

    If someText.Contains(someWord) Then
        Response.Write("Word found.")
    Else
        Response.Write("No word found.")
    End If


 Function Contains looks only for next words from left to right. 

someText.Contains("Over Stack")  returns False
someText.Contains("Stack Community")  returns False

只要字符串中存在单词,我希望所有这些都返回 True。

是否有任何现有函数可以覆盖任何情况,无论字符串中的单词位置如何?

C# or VB.NET suggestion are welcome.

I have the following code:

 Dim someText = "Stack Over Flow Community"
    Dim someWord = "Over Community"

    If someText.Contains(someWord) Then
        Response.Write("Word found.")
    Else
        Response.Write("No word found.")
    End If


 Function Contains looks only for next words from left to right. 

someText.Contains("Over Stack")  returns False
someText.Contains("Stack Community")  returns False

I want all of these to return True as long as there are words that exist in the string.

Is there any existing function that cover any case regardless of words position in the string?

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

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

发布评论

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

评论(4

筱武穆 2024-11-13 14:09:17
words.Split(' ').Any(someText.Contains)
words.Split(' ').Any(someText.Contains)
征﹌骨岁月お 2024-11-13 14:09:17

someText.Split(' ').Intersect(someWord.Split(' ')).Any();

someText.Split(' ').Intersect(someWord.Split(' ')).Any();

闻呓 2024-11-13 14:09:17
public static class StringExtension
{
    public static bool ContainsWord(this string source, string contain)
    {
        List<string> sourceList = source.Split(' ').ToList();
        List<string> containList = contain.Split(' ').ToList();

        return sourceList.Intersect(containList).Any();
    }
}


string someText = "Stack Over Flow Community";
var res = someText.ContainsWord("Over Stack");          // return true
var res1 = someText.ContainsWord("Stack Community");    // return true
var res2 = someText.ContainsWord("Stack1 Community");   // return false
public static class StringExtension
{
    public static bool ContainsWord(this string source, string contain)
    {
        List<string> sourceList = source.Split(' ').ToList();
        List<string> containList = contain.Split(' ').ToList();

        return sourceList.Intersect(containList).Any();
    }
}


string someText = "Stack Over Flow Community";
var res = someText.ContainsWord("Over Stack");          // return true
var res1 = someText.ContainsWord("Stack Community");    // return true
var res2 = someText.ContainsWord("Stack1 Community");   // return false
腹黑女流氓 2024-11-13 14:09:17

Linq 解决方案的替代方案是扩展方法:

public static bool ContainsAllWords(this string input, string search)
{
    foreach (string word in search.split(' ', StringSplitOptions.RemoveEmptyEntries))
    {
        if (!input.Contains(word))
            return false;
    }
    return true;
}

用法:

string test = "stack overflow";
string searchPhrase = "overflow stack";
Console.WriteLine(test.ContainsAllWords(searchPhrase));

更好的可重用性,并使您的代码更具声明性(恕我直言)。

An alternative to the Linq solutions would be an extension method:

public static bool ContainsAllWords(this string input, string search)
{
    foreach (string word in search.split(' ', StringSplitOptions.RemoveEmptyEntries))
    {
        if (!input.Contains(word))
            return false;
    }
    return true;
}

Usage:

string test = "stack overflow";
string searchPhrase = "overflow stack";
Console.WriteLine(test.ContainsAllWords(searchPhrase));

Better for re-usability and makes your code more declarative (imho).

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