Re: Implement a Case INsensitive string.Comtains method?
- From: Hans Kesting <news.hansdk@xxxxxxxxxxxxxxx>
- Date: Tue, 25 Aug 2009 12:22:46 +0200
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
.
- Follow-Ups:
- Re: Implement a Case INsensitive string.Comtains method?
- From: Joe Cool
- Re: Implement a Case INsensitive string.Comtains method?
- From: Finn Stampe Mikkelsen
- 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
- Re: Implement a Case INsensitive string.Comtains method?
- From: Joe Cool
- Implement a Case INsensitive string.Comtains method?
- Prev by Date: Enumerating public constants from class
- Next by Date: Re: Implement a Case INsensitive string.Comtains method?
- 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
|