From: Morten Wennevik [C# MVP] on
Hi,

It's very easy, actually

List<string> list = new List<string> { "hello" };
bool b = list.Contains("Hello", StringComparer.CurrentCultureIgnoreCase);

--
Happy Coding!
Morten Wennevik [C# MVP]


"mp" wrote:

> Is there a way to make String.Contains do a case insensitive comparison?
> like an equivalent of vb6 Option CompareText in a code module?
> I've read the help on the overload of .Contains that uses a
> IEqualityComparer object but don't understand how to make that work
> from the help:
> <quote
> This interface allows the implementation of customized equality comparison
> for collections. That is, you can create your own definition of equality for
> type T, and specify that this definition be used with a collection type that
> accepts the IEqualityComparer<(Of <(T>)>) generic interface. In the .NET
> Framework, constructors of the Dictionary<(Of <(TKey, TValue>)>) generic
> collection type accept this interface.
>
> end quote>
>
> but i'm comparing a string and a substring rather than a collection so don't
> know enough how to translate that to
>
> for example in the following snippet how can i eliminate the else if by
> making the comparison case insensitive
>
> particularly if the string is not hard coded but comes from a run time
> variable
>
> string line;
>
> //populate line variable
>
> //look for substring
>
> if (line.Contains("defun" ))
>
> {
>
> listBox1.Items.Add(line);
>
> }
>
> else if (line.Contains("Defun"))
>
> {
>
> listBox1.Items.Add(line);
>
> }
>
>
>
>
From: Morten Wennevik [C# MVP] on
Oh, my bad,

I thought you said list.Contains()

String Contains is almost as easy, although you are forced to write your own
extension as the provided comparer can only to a single char comparison.

Writing your own extensions is easy, though:

static class MyExtensions
{
public static bool ContainsIgnoreCase(this string source, string target)
{
return source.ToUpper().Contains(target.ToUpper());
}
}

....

string s = "Hello";
bool b = s.ContainsIgnoreCase("ELLO");

--
Happy Coding!
Morten Wennevik [C# MVP]


"Morten Wennevik [C# MVP]" wrote:

> Hi,
>
> It's very easy, actually
>
> List<string> list = new List<string> { "hello" };
> bool b = list.Contains("Hello", StringComparer.CurrentCultureIgnoreCase);
>
> --
> Happy Coding!
> Morten Wennevik [C# MVP]
>
>
> "mp" wrote:
>
> > Is there a way to make String.Contains do a case insensitive comparison?
> > like an equivalent of vb6 Option CompareText in a code module?
> > I've read the help on the overload of .Contains that uses a
> > IEqualityComparer object but don't understand how to make that work
> > from the help:
> > <quote
> > This interface allows the implementation of customized equality comparison
> > for collections. That is, you can create your own definition of equality for
> > type T, and specify that this definition be used with a collection type that
> > accepts the IEqualityComparer<(Of <(T>)>) generic interface. In the .NET
> > Framework, constructors of the Dictionary<(Of <(TKey, TValue>)>) generic
> > collection type accept this interface.
> >
> > end quote>
> >
> > but i'm comparing a string and a substring rather than a collection so don't
> > know enough how to translate that to
> >
> > for example in the following snippet how can i eliminate the else if by
> > making the comparison case insensitive
> >
> > particularly if the string is not hard coded but comes from a run time
> > variable
> >
> > string line;
> >
> > //populate line variable
> >
> > //look for substring
> >
> > if (line.Contains("defun" ))
> >
> > {
> >
> > listBox1.Items.Add(line);
> >
> > }
> >
> > else if (line.Contains("Defun"))
> >
> > {
> >
> > listBox1.Items.Add(line);
> >
> > }
> >
> >
> >
> >
From: Peter Duniho on
On Tue, 22 Sep 2009 23:17:01 -0700, Morten Wennevik [C# MVP]
<MortenWennevik(a)hotmail.com> wrote:

> [...]
> static class MyExtensions
> {
> public static bool ContainsIgnoreCase(this string source, string
> target)
> {
> return source.ToUpper().Contains(target.ToUpper());
> }
> }

I reiterate, one more time: do not use case conversions as a way of
implementing case-insensitive comparisons. It's unreliable as a
general-purpose solution.
From: Morten Wennevik [C# MVP] on
I disagree.

The articles you refer to claims case rules in .Net breaks for Turkish. As
far as I can tell, my code works as expected, even for Turkish.

The letter 'i' is equal to lower case 'I' in english Culture, but not in
Turkish culture.
If a Turkish word containing 'i' was input into an application running
English culture The application should follow English casing rules. If the
application was meant to work in Turkey, the application should use the
Turkish culture. If the application needs to server mixed cultures, case
comparison between cultures is a bad idea no matter how you do it.

If there are cultures where ToUpper or ToLower that would be a bug and
should be fixed.
--
Happy Coding!
Morten Wennevik [C# MVP]


"Peter Duniho" wrote:

> On Tue, 22 Sep 2009 23:17:01 -0700, Morten Wennevik [C# MVP]
> <MortenWennevik(a)hotmail.com> wrote:
>
> > [...]
> > static class MyExtensions
> > {
> > public static bool ContainsIgnoreCase(this string source, string
> > target)
> > {
> > return source.ToUpper().Contains(target.ToUpper());
> > }
> > }
>
> I reiterate, one more time: do not use case conversions as a way of
> implementing case-insensitive comparisons. It's unreliable as a
> general-purpose solution.
>
From: mp on
"Sreenivas" <thatiparthysreenivas(a)gmail.com> wrote in message
news:55e45b03-9642-4832-be71-e579c50d18f0(a)y10g2000prg.googlegroups.com...
On Sep 23, 1:34 am, "mp" <nos...(a)Thanks.com> wrote:
> Is there a way to make String.Contains do a case insensitive comparison?
I think using RegularExpressions are overkill ,in this case .
do in this way,
if(line.ToLower().Contains())
{
listBox1.Items.Add(line);
}


that's a good idea, thanks
mark