String.Contains之类的功能 [英] String.Contains like functionality

查看:87
本文介绍了String.Contains之类的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Convert.ToString((p.Category.ToString().ToUpper().Contains(fieldValue.ToString().ToUpper())))





我的字符串就像我的名字是汗



我遇到的问题是这个如果我比较我的名字或名字是,方法工作正常,但我想要的是一个方法,即使我写我是或我的汗,当前返回false时返回true。



Hi, I have string like "My name is khan"

problem that I facing is that this method works fine if I compare "My name" or "name is" but what I want is a method that returns true even if I write "My is" or "My khan" which at current return false.

推荐答案

您好,

您可以使用RegEx实现这一目标。



请参考以下网址< br $>


http:// stackoverflow .com / questions / 5417070 / c-sharp-version-of-sql-like [ ^ ]


你需要用空白字符拆分你的搜索字符串,然后根据主要字符检查它们ST一个接一个地振铃。

如果在主字符串中找到所有这些,你可以返回 true
You will need to split up your search string by whitespace characters and then check them against the main string one by one.
If all of them are found inside the main string, you can return true.


在此处查看: http://msdn.microsoft.com/en-us/library/bb546163。 aspx [ ^ ]

此示例使用LINQ to Objects,并且完全按照您的要求执行。不完全是因为它区分大小写,但你也可以用ToUpper克服这个问题。这个解决方案不会考虑字序。



在我看来,正则表达式不是正确的工具,因为我看到,你想动态匹配输入主题词的单词。所以你需要动态地构建regexp。如果单词顺序无关紧要,可能会很复杂。这可以实现,但如果这是一项经常性的任务,它可能会带来很大的开销。我会考虑regexp,只要它是一个很少执行的任务,并且单词的顺序很重要。但即使列表到列表匹配也会消耗更少的资源,而且会更快。
Look here: http://msdn.microsoft.com/en-us/library/bb546163.aspx[^]
This sample is using LINQ to Objects, and does exactly what you want. Well not exactly since it is case sensitive, but you can overcome this too with ToUpper. This solution does not take word order in account.

In my opinion regex wouldn''t be the right tool, because as I see, you want to dynamically match input words with subject words. So you would need to build regexp also dynamically. And could be complex if word order does not matter. That can be achieved, but if this is a frequent task, it could present big overhead. I would consider regexp only if it is a rarely executed task and the order of the words does matter. But even than a list-to-list matching would consume less resources and would be much faster.


这篇关于String.Contains之类的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆