Re: Implement a Case INsensitive string.Comtains method?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Aug 23, 11:50 pm, "Peter Duniho" <no.peted.s...@xxxxxxxxxxxxxxxxxx>
wrote:
On Sun, 23 Aug 2009 19:27:47 -0700, Joe Cool <joecool1...@xxxxxxxx> wrote:
I would lke to be able to do a case insentive search of a string. I
know this can be done using the VB InStr function, but I refuse to do
it that way. The proper .NET way is to write a custom string class
that exposes an override to the Contains method. Only I am having
problems getting started.

I know I need a class library tp put the code into. But what type of
project do I create? And what class do I need to inherit? Seems that
string is not a choice according to intellisense.

You may or may not need a different project.  It might be sufficient to  
simply create a new class in your current project, depending on whether  
you anticipate reusing this in other projects.  If you do create another  
project, you'll want to create a C# DLL/class library project (I forget  
what the exact terminology in the IDE is, but it something like that).

As far as inheriting goes, you shouldn't need to inherit any class.  You  
can't inherit System.String, because that class is sealed.  If you still  
want String-instance semantics, you can write an extension method for  
String.  For example:

     static class MyExtensions
     {
         public static bool Contains(this string strSearch, string strFind,  
StringComparison sc)
         {
             if (strFind.Length == 0)
             {
                 return true;
             }

             for (int ich = 0; ich < strSearch.Length - strFind.Length;  
ich++)
             {
                 if (strSearch.Substring(ich,  
strFind.Length).Equals(strFind, sc))
                 {
                     return true;
                 }
             }

             return false;
         }
     }

Then you can use it like this:

     string strT = "My dog has fleas";

     Console.WriteLine("My string has 'FLEAS': {0}",
         strT.Contains("FLEAS", StringComparison.CurrentCultureIgnoreCase));

Any variation on that theme would do also.

I tried to implement this but I get a build error, says it can't find
the type System.Runtime.CompilerServices.ExtensionAttribute. Says I am
missing a reference to System.Core.dll. But when I try to add a
reference, System.Core is grayed out.

???
.



Relevant Pages

  • Re: Combining Text based on Values
    ... keeping column A as reference to names. ... Function concatAs String ... Dim cell As Range ...     For Each cell In CellBlock ...
    (microsoft.public.excel.programming)
  • Re: Some newbie questions
    ... inherit from DataSet, DataSets inherit from Object if you go far enough up ... > To my suprise was the datacolumn indexer the fastest. ... --I'd expect the string to be the slowest but the indexer part is what's ...
    (microsoft.public.dotnet.framework.adonet)
  • setting property value erors in serviced components
    ... when I inherit the class from the ... ByVal sigortalituru As String, ByVal karneno As Integer, ByVal pgmid As ... ByVal unitekod As Integer, ByVal kaydet As Boolean) As ObjectImplements ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Implement a Case INsensitive string.Comtains method?
    ... The proper .NET way is to write a custom string class ... As far as inheriting goes, you shouldn't need to inherit any class. ... you can write an extension method for   ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: create reference number based on old one...
    ... I'm trying to create a function that will create a "reference number" ... You will need to worry about what happens when your code is restarted, ...     Function NewID(ByVal s As String) As String ...
    (microsoft.public.dotnet.languages.vb)