Click here to Skip to main content
15,895,781 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
C#
Convert.ToString((p.Category.ToString().ToUpper().Contains(fieldValue.ToString().ToUpper())))


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.
Posted

Hi,
You can achieve that using RegEx.

Please refer the below URL

http://stackoverflow.com/questions/5417070/c-sharp-version-of-sql-like[^]
 
Share this answer
 
Comments
Kishor Deshpande 2-Feb-13 14:42pm    
RegEx is the best option, to achieve this kind of functionality, you will have to customise.
My 5
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.
 
Share this answer
 
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.
 
Share this answer
 
v2
You can create and test your regex using online tool

http://gskinner.com/RegExr/[^]
 
Share this answer
 
Check out this article by Microsoft on regular expressions http://support.microsoft.com/kb/308252[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900