使用 LINQ 在字符串中查找单词
欢迎使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
someText.Split(' ').Intersect(someWord.Split(' ')).Any();
someText.Split(' ').Intersect(someWord.Split(' ')).Any();
Linq 解决方案的替代方案是扩展方法:
用法:
更好的可重用性,并使您的代码更具声明性(恕我直言)。
An alternative to the Linq solutions would be an extension method:
Usage:
Better for re-usability and makes your code more declarative (imho).