How to ignore the case sensitivity in List<string>


C# Problem Overview

Let us say I have this code

string seachKeyword = "";
List<string> sl = new List<string>();
List<string> searchResults = sl.FindAll(s => s.Contains(seachKeyword));

How can I ignore the letter case in Contains search?


C# Solutions

Solution 1 - C#

Use Linq, this adds a new method to .Compare

using System.Linq;
using System.Collections.Generic;

List<string> MyList = new List<string>();
if (MyList.Contains(TestString, StringComparer.CurrentCultureIgnoreCase)) {

so presumably

using System.Linq;

List<string> searchResults = sl.FindAll(s => s.Contains(seachKeyword, StringComparer.CurrentCultureIgnoreCase));  

Solution 2 - C#

The best option would be using the ordinal case-insensitive comparison, however the Contains method does not support it.

You can use the following to do this:

sl.FindAll(s => s.IndexOf(searchKeyword, StringComparison.OrdinalIgnoreCase) >= 0);

It would be better to wrap this in an extension method, such as:

public static bool Contains(this string target, string value, StringComparison comparison)
    return target.IndexOf(value, comparison) >= 0;

So you could use:

sl.FindAll(s => s.Contains(searchKeyword, StringComparison.OrdinalIgnoreCase));

Solution 3 - C#

You CAN use Contains by providing the case-insensitive string equality comparer like so:

if (myList.Contains(keyword, StringComparer.OrdinalIgnoreCase))
    Console.WriteLine("Keyword Exists");

Solution 4 - C#

StringComparer.CurrentCultureIgnoreCase is a better approach instead of using indexOf.

Solution 5 - C#

The optimal solution will be to ignore the case when performing the comparison

List<string> searchResults = sl.FindAll(s => s.IndexOf(seachKeyword, System.StringComparison.OrdinalIgnoreCase) >= 0);

Solution 6 - C#

Below method will search your needed keyword and insert all searched items into a new list and then returns new list.

private List<string> serchForElement(string searchText, list<string> ListOfitems)
    searchText = searchText.ToLower();
    List<string> Newlist = (from items in ListOfitems
                         where items.ToLower().Contains(searchText.ToLower())
                         select items).ToList<string>(); 

return Newlist; }

Solution 7 - C#

In the latest support we can have similar condition checked in LINQ even much more simple and elegant

using System.Linq

var strToCompare = "Hello";
bool match = myList.Any(x => string.Compare(x, strToCompare, StringComparison.OrdinalIgnoreCase) == 0);

Solution 8 - C#

You can apply little trick over this.
Change all the string to same case: either upper or lower case

List searchResults = sl.FindAll(s => s.ToUpper().Contains(seachKeyword.ToUpper()));

Solution 9 - C#

For those of you having problems with searching through a LIST of LISTS, I found a solution.

In this example I am searching though a Jagged List and grabbing only the Lists that have the first string matching the argument.

List<List<string>> TEMPList = new List<List<string>>();

TEMPList = JaggedList.FindAll(str => str[0].ToLower().Contains(arg.ToLower()));


Solution 10 - C#

The FindAll does enumeration of the entire list.

the better approach would be to break after finding the first instance.

bool found = list.FirstOrDefault(x=>String.Equals(x, searchKeyWord, Stringcomparison.OrdinalIgnoreCase) != null;

Solution 11 - C#

Simply, you can use LINQ query as like below,

String str = "StackOverflow";
int IsExist = Mylist.Where( a => a.item.toLower() == str.toLower()).Count()
if(IsExist > 0)

Solution 12 - C#

One of possible (may not be the best), is you lowercase all of the strings put into sl. Then you lowercase the searchKeyword.

Another solution is writing another method that lowercase 2 string parameters and compares them


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarkView Question on Stackoverflow
Solution 1 - C#CestLaGalereView Answer on Stackoverflow
Solution 2 - C#Igal TabachnikView Answer on Stackoverflow
Solution 3 - C#GorvGoylView Answer on Stackoverflow
Solution 4 - C#Kunal GoelView Answer on Stackoverflow
Solution 5 - C#Petar PetrovView Answer on Stackoverflow
Solution 6 - C#Shakeeb MohammedView Answer on Stackoverflow
Solution 7 - C#DeveshView Answer on Stackoverflow
Solution 8 - C#DipitakView Answer on Stackoverflow
Solution 9 - C#Ken from northweststate CCView Answer on Stackoverflow
Solution 10 - C#savagepandaView Answer on Stackoverflow
Solution 11 - C#Jitendra G2View Answer on Stackoverflow
Solution 12 - C#vodkhangView Answer on Stackoverflow