Re: Implement a Case INsensitive string.Comtains method?

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



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.

Hans Kesting


.



Relevant Pages

  • 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. ... missing a reference to System.Core.dll. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: help to multiplication
    ... i need to multiple a string * 5 ...     What is the expected result? ... Note that my example is an Extension method, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: 11-string recuerdos- where on the bell curve?
    ...   Yes, a couple of the ornaments in the first section did not come out ... which there is not room for improvement, slurs included, but I do not ... This is my recording of Recuerdos done on six string several years ... a heavier bass? ...
    (rec.music.classical.guitar)
  • Re: Export multiple versions of a query to separate Excel files
    ... If I try to run the code I can select the folder that I want to create ... Dim qdf As DAO.QueryDef ... Dim strSQL As String, strTemp As String, strMgr As String ...
    (microsoft.public.access.externaldata)
  • Re: Using String for new List name
    ...     for line in infile: ... and aclS represents "access control list String." ... My quest, if you will, is to create a list of syslog entries, each ... Since ACL rules can be written with a range of granularity, ...
    (comp.lang.python)