PC Review


Reply
Thread Tools Rate Thread

CaseInsensitiveComparer with Generic List problem

 
 
Andrew Jocelyn
Guest
Posts: n/a
 
      10th Jul 2008
Hi

I need to do a case insensitive search on a Generic list. I'm trying to use
the BinarySearch method but I get a complier error "cannot convert from
'System.Collections.CaseInsensitiveComparer' to
'System.Collections.Generic.IComparer<string>' "

List<string> list = new List<string>();
list.Add(str1);
list.Add(str2);
list.Add(str3);
int index = list.BinarySearch(otherstr, new CaseInsensitiveComparer());

What's the correct procedure?

Thanks
Andrew


 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      10th Jul 2008
try: int index = list.BinarySearch(otherstr,
StringComparer.OrdinalIgnoreCase);

or InvariantCultureIgnoreCase or CurrentCultureIgnoreCase. Note that
binary search only works on sorted data; depending on what you are
doing, a SortedList<TKey,TValue> might also be worth considering,
which has an IndexOfKey method.

Marc
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      10th Jul 2008
Andrew Jocelyn <(E-Mail Removed)> wrote:
> I need to do a case insensitive search on a Generic list. I'm trying to use
> the BinarySearch method but I get a complier error "cannot convert from
> 'System.Collections.CaseInsensitiveComparer' to
> 'System.Collections.Generic.IComparer<string>' "
>
> List<string> list = new List<string>();
> list.Add(str1);
> list.Add(str2);
> list.Add(str3);
> int index = list.BinarySearch(otherstr, new CaseInsensitiveComparer());
>
> What's the correct procedure?


Use StringComparer.*IgnoreCase (where * is Ordinal, CurrentCulture or
InvariantCulture depending on your needs).

--
Jon Skeet - <(E-Mail Removed)>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
 
Reply With Quote
 
Nick Olsen
Guest
Posts: n/a
 
      24th Aug 2010
Here is a blog post that shows how to do a case insensitive search on a list of strings.

http://nickstips.wordpress.com/2010/...ntains-method/

Hope that helps!

Nick

> On Thursday, July 10, 2008 5:40 PM Andrew Jocelyn wrote:


> Hi
>
> I need to do a case insensitive search on a Generic list. I'm trying to use
> the BinarySearch method but I get a complier error "cannot convert from
> 'System.Collections.CaseInsensitiveComparer' to
> 'System.Collections.Generic.IComparer<string>' "
>
> List<string> list = new List<string>();
> list.Add(str1);
> list.Add(str2);
> list.Add(str3);
> int index = list.BinarySearch(otherstr, new CaseInsensitiveComparer());
>
> What's the correct procedure?
>
> Thanks
> Andrew



>> On Thursday, July 10, 2008 6:03 PM Jon Skeet [C# MVP] wrote:


>> Andrew Jocelyn <(E-Mail Removed)> wrote:
>>
>> Use StringComparer.*IgnoreCase (where * is Ordinal, CurrentCulture or
>> InvariantCulture depending on your needs).
>>
>> --
>> Jon Skeet - <(E-Mail Removed)>
>> Web site: http://www.pobox.com/~skeet
>> Blog: http://www.msmvps.com/jon_skeet
>> C# in Depth: http://csharpindepth.com



>>> On Monday, July 14, 2008 1:13 AM Marc Gravell wrote:


>>> try: int index = list.BinarySearch(otherstr,
>>> StringComparer.OrdinalIgnoreCase);
>>>
>>> or InvariantCultureIgnoreCase or CurrentCultureIgnoreCase. Note that
>>> binary search only works on sorted data; depending on what you are
>>> doing, a SortedList<TKey,TValue> might also be worth considering,
>>> which has an IndexOfKey method.
>>>
>>> Marc



>>> Submitted via EggHeadCafe - Software Developer Portal of Choice
>>> Assemblies in Folder Debug Build Checker
>>> http://www.eggheadcafe.com/tutorials...d-checker.aspx

 
Reply With Quote
 
Nick Olsen
Guest
Posts: n/a
 
      24th Aug 2010
Here is an example of how to write a case insensitive Contains method on a list of strings.

http://nickstips.wordpress.com/2010/...ntains-method/

Hope it helps!

Nick

> On Thursday, July 10, 2008 5:40 PM Andrew Jocelyn wrote:


> Hi
>
> I need to do a case insensitive search on a Generic list. I'm trying to use
> the BinarySearch method but I get a complier error "cannot convert from
> 'System.Collections.CaseInsensitiveComparer' to
> 'System.Collections.Generic.IComparer<string>' "
>
> List<string> list = new List<string>();
> list.Add(str1);
> list.Add(str2);
> list.Add(str3);
> int index = list.BinarySearch(otherstr, new CaseInsensitiveComparer());
>
> What's the correct procedure?
>
> Thanks
> Andrew



>> On Thursday, July 10, 2008 6:03 PM Jon Skeet [C# MVP] wrote:


>> Andrew Jocelyn <(E-Mail Removed)> wrote:
>>
>> Use StringComparer.*IgnoreCase (where * is Ordinal, CurrentCulture or
>> InvariantCulture depending on your needs).
>>
>> --
>> Jon Skeet - <(E-Mail Removed)>
>> Web site: http://www.pobox.com/~skeet
>> Blog: http://www.msmvps.com/jon_skeet
>> C# in Depth: http://csharpindepth.com



>>> On Monday, July 14, 2008 1:13 AM Marc Gravell wrote:


>>> try: int index = list.BinarySearch(otherstr,
>>> StringComparer.OrdinalIgnoreCase);
>>>
>>> or InvariantCultureIgnoreCase or CurrentCultureIgnoreCase. Note that
>>> binary search only works on sorted data; depending on what you are
>>> doing, a SortedList<TKey,TValue> might also be worth considering,
>>> which has an IndexOfKey method.
>>>
>>> Marc



>>>> On Tuesday, August 24, 2010 6:43 PM Nick Olsen wrote:


>>>> Here is a blog post that shows how to do a case insensitive search on a list of strings.
>>>>
>>>>
>>>>
>>>> http://nickstips.wordpress.com/2010/...ntains-method/
>>>>
>>>>
>>>>
>>>> Hope that helps!
>>>>
>>>>
>>>>
>>>> Nick



>>>> Submitted via EggHeadCafe - Software Developer Portal of Choice
>>>> Composite UI Pattern and RAD Development for Data Entry Applications, Part 1
>>>> http://www.eggheadcafe.com/tutorials...ns-part-1.aspx

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Generic List Remove method Joe Cool Microsoft C# .NET 2 20th Jun 2010 12:57 AM
<newbie> generic list problem Jeff Microsoft C# .NET 6 20th Jul 2007 10:27 AM
Generic List Problem Radenko Zec Microsoft C# .NET 1 16th Jul 2007 10:35 AM
Re: Problem with generic List<> Carl Daniel [VC++ MVP] Microsoft Dot NET 1 17th Nov 2006 08:18 PM
Re: Problem with generic List<> Carl Daniel [VC++ MVP] Microsoft Dot NET Framework 1 17th Nov 2006 08:18 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:32 AM.