Click here to Skip to main content
15,886,676 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i had get the one column values from database that value contains another string or not

code
C#
String.Contains(business.Cuisine,"chicken",StringComparison.OrdinalIgnoreCase)


contains method not shown intelisense report thats why i have implement contains method
This method not working propely.For suppose if the source is null. how to handle and ignore the case.

C#
public static bool Contains(this string source, string toCheck, StringComparison comp)
      {
          if (source == null)
              return false;
          return source.IndexOf(toCheck, comp) >= 0;
      }


Please help me.
Thank u.

What I have tried:

C#
public static bool Contains(this string source, string toCheck, StringComparison comp)
      {
          if (source == null)
              return false;
          return source.IndexOf(toCheck, comp) >= 0;
      }
Posted
Updated 25-Dec-18 20:00pm

Quote:
contains method not shown intelisense

That's because Contains is not a static method: it's an instance method so you call it on the actual string:
C#
if (business.Cuisine.Contains("chicken"))
   {
   ...

And there is no Contains method that allows you to specify case insensitivity.
But the IndexOf method deos, as you have found on the internet.

But as I explained to you when you were trying it with Regexes:
How to handle null values in regx.ismatch[^]
You need to check and handle nulls yourself! The system will not do that for you - that is by design, and is absolutely correct...

Please, after 104 questions, you should be pretty competent in this stuff now, not making exactly the same mistake over and over again ... which make you look like you are learning absolutely nothing here, and that would just make you a Help Vampire.
 
Share this answer
 
Comments
Nirav Prabtani 26-Dec-18 2:06am    
5+
String.IndexOf Method (System) | Microsoft Docs[^]
public static bool Contains(this string source, string toCheck, StringComparison comp)
      {
          if (source == null)
              return false;

          return source.IndexOf(toCheck, 0, source.Length, comp) >= 0;
      }
 
Share this answer
 
Comments
OriginalGriff 26-Dec-18 2:17am    
Reason for my vote of one: This is the code he has already found, and that is in his question...

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