Re: Implement a Case INsensitive string.Comtains method?
- From: Joe Cool <joecool1969@xxxxxxxx>
- Date: Mon, 24 Aug 2009 20:55:57 -0700 (PDT)
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.
???
.
- Follow-Ups:
- Re: Implement a Case INsensitive string.Comtains method?
- From: Hans Kesting
- Re: Implement a Case INsensitive string.Comtains method?
- References:
- Implement a Case INsensitive string.Comtains method?
- From: Joe Cool
- Re: Implement a Case INsensitive string.Comtains method?
- From: Peter Duniho
- Implement a Case INsensitive string.Comtains method?
- Prev by Date: Re: iLike written in PHP (not c#)
- Next by Date: Re: Test driven development - dependened on database entries with c#
- Previous by thread: Re: Implement a Case INsensitive string.Comtains method?
- Next by thread: Re: Implement a Case INsensitive string.Comtains method?
- Index(es):
Relevant Pages
|