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

7
  • possible duplicate of A case-insensitive list Commented Jun 11, 2013 at 7:54
  • Case insensitive contains: stackoverflow.com/questions/444798/… Commented Jun 11, 2013 at 7:56
  • 2
    Are you confusing invariant with insensitive? Commented Jun 11, 2013 at 8:01
  • 1
    Your examples don't need to use Contains. Should "TeSt1" match "tEsT12"? Commented Jun 11, 2013 at 8:04
  • @ric this is a another topic. it is not if a string contains a substirng like your link. Commented Jun 11, 2013 at 8:09

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

But won't that also match for substrings?
@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.
Doh, completely missed that, misread that as collection contains search string.
@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
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
}

3 Comments

But that would match if the match was a substring of the other
@BobVale Since the OP used Contains in his original code, I'm assuming that's his intent.
Doh, completely missed that, misread that as collection contains search string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.