1

The following code could be used to search in an array / List of strings using LINQ.

String[] someArray
    = { "Test1", "test2", "test3", "TEST4" };

string toCheck = "Test1";

if (someArray.Any(toCheck.Contains))
{
    // found -> Do sth.
}
// or with list 

List<string> someList
    = new List<string>(new string[] { "TEST1", "test2", "test3", "TEST4"  });

if (someList.Any(toCheck.Contains))
{
    // "Test1" != "TEST1"
}

But how could you do this case invariant?

My approach was to convert the complete list to upper, and then test using contains:

if ((someList.ConvertAll(item => item.ToUpper()).Any(toCheck.ToUpper().Contains)))
{
    // found -> Do sth.
}

In this case the original list is not altered.

if ((someList.Select(item => item.ToUpper()).Any(toCheck.ToUpper().Contains)))
{
    // works with both
}

Well it works... (also with some language specific things like the turkish 'i' letter... (also we still don't have turkish customers as far as i know.. but who knows if they're are in future?)), but it seems not very elegant.

Is there a way to do an case invariant comparision if an item is in a list?

Best regards, Offler

Offler
  • 1,223
  • 1
  • 12
  • 34
  • possible duplicate of [A case-insensitive list](http://stackoverflow.com/questions/1530748/a-case-insensitive-list) – Alex Filipovici Jun 11 '13 at 07:54
  • Case insensitive contains: http://stackoverflow.com/questions/444798/case-insensitive-containsstring?rq=1 – Ric Jun 11 '13 at 07:56
  • 2
    Are you confusing invariant with insensitive? – Jodrell Jun 11 '13 at 08:01
  • 1
    Your examples don't need to use `Contains`. Should `"TeSt1"` match `"tEsT12"`? – Jodrell Jun 11 '13 at 08:04
  • @ric this is a another topic. it is not if a string contains a substirng like your link. – Offler Jun 11 '13 at 08:09
  • @AlexFilipovici The list itself should not be altered. It is only about a method to find a stirng in a list or array or ... – Offler Jun 11 '13 at 08:11
  • @jodrell Contains is only needed as the result should be true in case of substrings. Ans yes in this case it should match, good comment, forgotten to put it in the post. – Offler Jun 11 '13 at 08:19

2 Answers2

3

Instead of Contains use IndexOf with StringComparison.OrdinalIgnoreCase:

String[] strings = { "Test1", "test2", "test3", "TEST4" };
String text = "TEST123";
if (strings.Any(str => text.IndexOf(str, StringComparison.OrdinalIgnoreCase) > -1))
{
    // we will enter this if clause
}

Demo

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 1
    But won't that also match for substrings? – Bob Vale Jun 11 '13 at 08:02
  • @BobVale: Yes, that's what OP wants, the same what `Contains` does. Actually `String.Contains` uses `IndexOf` internally. I've edited my answer to clarify what it does and to add a demo. – Tim Schmelter Jun 11 '13 at 08:03
  • Doh, completely missed that, misread that as collection contains search string. – Bob Vale Jun 11 '13 at 08:05
  • @BobVale yes, missed to put it in the question, was to focussed on a specific solution. If an exact match would be needed Tims solution could be changed to `Equals` instead of `IndexOf`, but `IndexOf` is the right one for my purpose – Offler Jun 11 '13 at 08:23
1

Could you not just do a simple check with the IndexOf that uses the appropriate StringComparison value? For example:

if(someArray.Any(s => s.IndexOf(toCheck, StringComparison.CurrentCultureIgnoreCase) != -1)
{
    // do something
}
brianestey
  • 8,202
  • 5
  • 33
  • 48