From: mp on
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: Sreenivas on
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?
> 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);
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -


Go for Regular Expressions,

using System.Text.RegularExpressions;

if( Regex.IsMatch(inputStringObject,
stringToMatch,RegexOptions.IgnoreCase) )
{

}
From: Sreenivas on
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?
> 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);
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -
I think using RegularExpressions are overkill ,in this case .
do in this way,
if(line.ToLower().Contains())
{
listBox1.Items.Add(line);
}

From: Peter Duniho on
On Tue, 22 Sep 2009 13:34:03 -0700, mp <nospam(a)thanks.com> 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?

It is odd that Contains() doesn't include an overload that takes a
StringComparison value. It's so odd, we only a month ago had this same
discussion:
http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_thread/thread/ebc4258ed2c4270f/c48b6c15df01e8bf

> 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:

Careful there. That's not an overload. It's an extension method. The
String class has only one Contains() method. But, String implements
IEnumerable<Char>, and the Enumerable class has a Contains<T>() extension
method that can be used with an IEnumerable<Char>.

As for the IEqualityComparer<T> interface:

> <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

Actually, if you use the Enumerable.Contains() method, you are comparing a
collection, even if you didn't realize it. :) But, this particular
method isn't useful as a replacement for String.Contains(), because it can
only find a single element in the collection, and a single element is just
one character.

Even if Enumerable contained a method that allowed you to look for a
subsequence in a given sequence, there would be other issues related to
implementing the IEqualityComparer<char> interface. But those issues are
moot, since as far as I can tell from your question, you are looking to
search for multi-character strings within other strings.

Anyway, hopefully the previous message thread we had last month will be
useful to you.

Pete
From: Peter Duniho on
On Tue, 22 Sep 2009 22:15:46 -0700, Sreenivas
<thatiparthysreenivas(a)gmail.com> wrote:

> I think using RegularExpressions are overkill ,in this case .
> do in this way,
> if(line.ToLower().Contains())
> {
> listBox1.Items.Add(line);
> }

Regex might be a little bit of overkill as compared to the most efficient
alternatives, but simply changing the case is a much poorer solution.
Please refer to the previous discussion (I provided a link in my other
reply to the OP) for details on why.

I would definitely use Regex before I would be willing to simply convert
the case.

Pete