-1

I have a list of words like so :

List<string> list = new List<string>();
list.Add("Horse");
list.Add("Shorse"):

I want to search the list for a specific string, regardless of case, but it has to be an EXACT match, if i do

if (list.Contains("horse",StringComparer.CurrentCultureIgnoreCase)) 
{ 
    //do something
}

It will find both Horse and Shorse.

How can i implement my own custom Contains method that will find an exact match?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
scseano
  • 21
  • 1
  • 1
  • 2
  • 2
    "regardless of case, but it has to be an EXACT match" those are mutually-exclusive. – Dai Mar 11 '16 at 23:26
  • 6
    No, this will not find both Horse and Shorse. – venerik Mar 11 '16 at 23:26
  • If by an exact match you mean the whole string in each index of the list then just do a for loop and compare each list item to the string you want to find. – EDD Mar 11 '16 at 23:28
  • 3
    `list.Contains` only returns a yes or a no. If you search for either `"Horse"` or `"Shorse"` in your example list, of course you will both get a true result, since both are in the list. And no, searching for `"horse"` would not yield a positive result if there was only `"Shorse"` in the list. – poke Mar 11 '16 at 23:29
  • 1
    It will find `Horse` or `horse` or `hOrSe` whatever in lower case equal to `horse`. It won't find `Shorse` or whatever that in lower case is not equal to `horse`. – Valentin Mar 11 '16 at 23:33
  • `if (list.Any(x => string.Compare(x, "horse", UseAnyOptionYouWant))) { }` – Eser Mar 11 '16 at 23:39
  • I'd recommend to all readers and answers, please read [The Turkish i Problem and Why you should care](http://haacked.com/archive/2012/07/05/turkish-i-problem-and-why-you-should-care.aspx/). – Erik Philips Mar 11 '16 at 23:50
  • I just tested my answer and it works - case sensitive so why vote down while it's the answer?? – Khalil Khalaf Mar 11 '16 at 23:55

4 Answers4

5

You are already correctly looking for exact matches in your list. The only thing which you explicitly specified is that you want to ignore the case of the matches. So if you have a Horse in the list, you can also find it as horse, or hOrsE. But you cannot find it as orse:

List<string> list = new List<string>();
list.Add("Horse");
list.Add("Shorse");

// we can find it with different casing
Console.WriteLine(list.Contains("horse", StringComparer.CurrentCultureIgnoreCase)); // true
Console.WriteLine(list.Contains("shorse", StringComparer.CurrentCultureIgnoreCase)); // true

// but not elements that are not in the list
Console.WriteLine(list.Contains("orse", StringComparer.CurrentCultureIgnoreCase)); // false

// if we don’t want to ignore the case, we can also do that
Console.WriteLine(list.Contains("Horse")); // true
Console.WriteLine(list.Contains("Shorse")); // true
Console.WriteLine(list.Contains("horse")); // false
Console.WriteLine(list.Contains("shorse")); // false

// and let’s look at a list with only Shorse to be sure…
list.Clear();
list.Add("Shorse");
Console.WriteLine(list.Contains("horse")); // false
poke
  • 369,085
  • 72
  • 557
  • 602
3

You could just make both your strings lowercase (or uppercase, whichever floats your boat) and compare them. You can do so easily using Any instead of Contains:

List<string> list = new List<string>();
list.Add("Horse");
list.Add("Shorse");

var needle = "horse";
var contains = list.Any(x => x.ToLower() == needle.ToLower()); // true
Gediminas Masaitis
  • 3,172
  • 14
  • 35
  • @Eser Don't think there's a reason - looking at this question there was a massive spree of random downvotes. And I see COLDTOLD also posted the same solution. Which I didn't notice, because it was downvoted into oblivion, heh. This happens from time to time, never understood why though. – Gediminas Masaitis Mar 11 '16 at 23:49
  • 1
    Because of the [Turkish i problem](http://haacked.com/archive/2012/07/05/turkish-i-problem-and-why-you-should-care.aspx/). Use the [StringComparison](https://msdn.microsoft.com/en-us/library/system.stringcomparison(v=vs.110).aspx) enum (which was built for a reason) is the correct way to compare strings when Ignoring Case. – Erik Philips Mar 11 '16 at 23:57
0

To find a match ignoring case you can do this:

var valueToMatch = "horse";

var matchedValue = list.FirstOrDefault(x => x.Equals(valueToMatch, StringComparison.OrdinalIgnoreCase);

Debug.WriteLine(matchedValue);
venerik
  • 5,766
  • 2
  • 33
  • 43
0

EDIT:

This is not an answer since I got the question the other way around. This answers a case sensitive situation. so if you lower cases all then check it should work.

TESTED!

Load your data and use this function to find the index of the exact sequence of characters in a list and it's case sensitive!

        //populate your main list
        List<string> YourList = new List<string>();
        YourList.Add("Horse");
        YourList.Add("Shorse");
        YourList.Add("HorseS");

        //find index of matching sequence of characters
        // this return 0
        int index = YourList.FindIndex(collection => collection.SequenceEqual("Horse"));
        Console.WriteLine(index);

        // this return 1
        index = YourList.FindIndex(collection => collection.SequenceEqual("Shorse"));
        Console.WriteLine(index);

        // this return -1 (not found)
        index = YourList.FindIndex(collection => collection.SequenceEqual("SHorse"));
        Console.WriteLine(index);

        // this return -1 (not found)
        index = YourList.FindIndex(collection => collection.SequenceEqual("horse"));
        Console.WriteLine(index);

        // this return 2
        index = YourList.FindIndex(collection => collection.SequenceEqual("HorseS"));
        Console.WriteLine(index);

And here is the output:

enter image description here

Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104