Click here to Skip to main content
12,354,330 members (70,117 online)
Rate this:
 
Please Sign up or sign in to vote.
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 2-Feb-13 7:37am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi,
You can achieve that using RegEx.

Please refer the below URL

http://stackoverflow.com/questions/5417070/c-sharp-version-of-sql-like[^]
  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

You can create and test your regex using online tool

http://gskinner.com/RegExr/[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 3 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100