Re: Implement a Case INsensitive string.Comtains method?

Tech-Archive recommends: Speed Up your PC by fixing your registry



On Aug 25, 6:22 am, Hans Kesting <news.han...@xxxxxxxxxxxxxxx> wrote:
Joe Cool has brought this to us :





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.

???

You are not using the 3.5 framework, which you need for the extension
methods to work (although there are tricks to work around that).
So switch your project to the 3.5 framework (see the properties) and it
will work.



Would have been nice if the help text, namely "How to: Implement and
Call a Custom Extension Method (C# Programming Guide)" had mentioned
that the topic applied only to .NER Frameword 3.5.

Anyhoo, that was it. I was working with a project I had converted from
VS2005 anf forgot to upgrade the framework version.
.



Relevant Pages

  • Re: some interesting perspectives on .NET from the other camp ...
    ... >> now rather use framework functionality directly. ... Do you really think a VB.NET writer is any more dependent on MS ... low platform or framework level functions altogether. ... Trimming a string just seems to me to be exactly ...
    (borland.public.delphi.non-technical)
  • Re: Is it really true that native English speakers cannot tell skill and sgill apart?
    ... nonsensical proper name. ... the hearer wouldn't come up with a string that didn't conform). ... It isn't that I'm hearing a string of syllables that I'm perceiving to be meaningless, and then, for the sake of being able to call the thing a mondegreen, claiming it to be a proper name. ... language, and a passage is a bit difficult, I notice how something is ...
    (sci.lang)
  • Re: IE Hosts Windows Control
    ... statement in the "What's new in the .NET Framework 1.1" ... Internet-based Web page or Windows Forms assemblies ... could get my control to execute on a framework v1 sp1 ... >> assemblyName, String ...
    (microsoft.public.dotnet.security)
  • RE: Outlook 2007 does not display ACCEPT|DECLINE buttons with an ICAL
    ... The code is sent both in a proper body-section with ... Gmail, Thunderbird and Outlook 2003. ... string Subject = this.CreateSubject; ... AlternateView calendarView = ...
    (microsoft.public.outlook.calendaring)
  • Extension Method Weirdness
    ... compiler grumpiness in one of our projects, ... /// Provides extension mthods for the String class. ... confused with the error that occurs when *a suitable extension method ... The DLL is appropriately referenced. ...
    (microsoft.public.dotnet.framework.aspnet)