How to get List of items from existing List matching a keyword and ignoring case (case-insensitive)?

c list<string contains case insensitive iequalitycomparer
c list<string contains case insensitive iequalitycomparer
case insensitive search in list python
c list except ignore case
case insensitive list search java
make string case insensitive
c# string contains case insensitive
python list in case insensitive

I am having a SeachBar in Xamarin Forms, and ListView that shows suggestions from existing list of cities. I want to get list of items matching search keyword irrespective of it being uppercase or lowercase.

For that I am having a List. I want to Search items from that list and get List of items that matched the search keyword ignoring the case. I am having a code for getting list of items that matched keyword. I just want it to include items that match search keyword ignoring the case of letters.

(Please note that here I want to return a List of the matched items and not bool whether match exists. So please do not close this question or mark similar to the one that returns bool.)

Here is my code

List<string> allCities = new List<string> { "Mumbai", "Redmond", "Cambridge", "London", "Moscow", "New York", "Chicago"};

void SearchList()
{
    string keyword = "mum";
    var citiesSearched = allCities.Where(c => c.Contains(keyword));
    ListView.ItemSource = citiesSearched;
}

I have tried with StringComparer.OrdinalIgnoreCase and StringComparison.OrdinalIgnoreCase but I was getting Error No overload for Contains takes 2 arguments.

This was the modification:

var citiesSearched = allCities.Where(c => c.Contains(keyword, StringComparer.OrdinalIgnoreCase));
// And
var citiesSearched = allCities.Where(c => c.Contains(keyword, StringComparison.OrdinalIgnoreCase));

in both the statements I got the error.

There's no overload of String.Contains that accepts a StringComparison or a StringComparer, but you can use IndexOf with a StringComparison, and use the fact that IndexOf returns -1 if the substring isn't found, or the index of the substring within the larger string otherwise. So you can use:

var citiesSearched = allCities.Where(
    c => c.IndexOf(SearchBar.Text, StringComparison.OrdinalIgnoreCase) != -1);

If you want it to be culture-sensitive for a specific CultureInfo, you can use CultureInfo.CompareInfo to get a CompareInfo, then CompareInfo.IndexOf with CompareOptions.IgnoreCase to perform a culture-sensitive case-insensitive search.

Contains And Ignore Case, If you want true case insensitive replace, check out hte following article:. Re: Find in list and ignore case Posted 20 May 2012 - 07:22 AM The problem with Option Description Syntax Restrictions; i: Case insensitivity to match upper and Sting value as a parameter, verifies whether the current String object contains the � Lookup case insensitive with Kutools for Excel If you have Kutools for Excel i nstalled, you can use the Look for a value in list function to quickly and easily lookup a value case insensitive. Kutools for Excel , with more than 300 handy functions, makes your jobs more easier.

Simple solution based on one of the fastest string API with StringComparison enum flag:

List<string> allCities = new List<string> { "Mumbai", "Redmond", "Cambridge", "London", "Moscow", "New York", "Chicago"};

void SearchList()
{
    string keyword = "mum";
    var citiesSearched = allCities.Where(c => c.IndexOf(keyword, StringComparison.OrdinalIgnoreCase) >= 0);
    ListView.ItemSource = new List(citiesSearched);
}

about_Switch, To check a condition in a script or function, use an If statement. The If statement The Switch statement lists each condition and an optional action. If a condition It performs a case-insensitive match for the value. If the value is a match exactly . If the match clause is not a string, this parameter is ignored. Case insensitive SQL SELECT query FAQ: How do I issue SQL SELECT queries while ignoring case (ignoring whether a string is uppercase or lowercase)?. Background. When I first started writing SQL queries I was using Postgresql, and used some of their custom regular expression capabilities to perform case-insensitive queries.

List<string> allCities = new List<string> { "Mumbai", "Redmond", "Cambridge", 
"London", "Moscow", "New York", "Chicago"};

void SearchList()
{
    string keyword = "mum";
    var citiesSearched = allCities.Where(c => c.ToLower().Contains(keyword.ToLower()));
    ListView.ItemSource = citiesSearched;
}

transform your text to lower case before the check should do the trick.

Compare strings (case insensitive) - MATLAB strcmpi, strcmpi returns 1 because s1 and s2 are equal when ignoring case. Find Case- Insensitive Match in Cell Array. As you remember, the EXACT function compares the value in cell F2 against all the items in column A and if an exact case-sensitive match is found, returns TRUE, otherwise FALSE. In mathematical operations, Excel treats TRUE as "1" and FALSE as "0", so the SUMPRODUCT function multiplies the returned numbers and sums the products.

Just convert to lower case (or upper case) each element of the list before calling Contains and also the keyword. the idea is to have both terms in common case:

var citiesSearched = allCities.Where(c => c.toLower().Contains(keyword.ToLower()));

Try it with:

string keyword = "mum";
List<string> allCities = new List<string> { "Mumbai", "numUmbus","Redmond", "Cambridge", "London", "Moscow", "New York", "Chicago"};
var citiesSearched = allCities.Where(c => c.ToLower().Contains(keyword.ToLower()));
foreach (var s in citiesSearched)
{
    Console.WriteLine(s);
}

String, Explicitly excluded words can be given as a list or as a string with words The reason this keyword does not use Python's standard title() method is that it can strict : fail if characters cannot be decoded (default); ignore : ignore Notice that to make the match case-insensitive, you need to prefix the pattern� No.. i need to have a for loop for each item in list 1 and need to check with whether list 1 item matches to any of the list 2 elements. ex: Foreach(List item in in _list1) {\ eed to check is there any value item.id, key , position values in _list2 and if it presents return amount, status of that list }

Case sensitivity, In computers, case sensitivity defines whether uppercase and lowercase letters are treated as Some other programming languages have varying case- sensitivity; in PHP, for and ignores underscores, as long as the first characters match. Main page � Contents � Current events � Random article � About Wikipedia� Allows me to create a master list and sub-lists that update when new items are added. It’s absolute gold! (Unfortunately, I’ll probably have a hard time convincing anyone in my industry (architecture) that its more useful than confusing lol…)

14 Strings, Have seven letters or more. Since this list is long, you might want to use the match argument to str_view() to show only the matching or non� And here is the case insensitive contains method implementation. public static class Extensions {public static bool CaseInsensitiveContains (this string text, string value, StringComparison stringComparison = StringComparison. CurrentCultureIgnoreCase) {return text. IndexOf (value, stringComparison) >= 0;}} And here is the modified tests.

Groovy Language Documentation, String interpolation; Special case of interpolating closure expressions The following list represents all the keywords of the Groovy language: def text = " some text to match" def m = text =~ /match/ (1) assert m instanceof Matcher (2) if (! m) Annotations from the collector will be ignored if any existing annotations with the� List class can be used to create a collection of different types like integers, strings etc. List class also provides the methods to search, sort, and manipulate lists. List.Count Property is used to get the total number of elements contained in the List.

Comments
  • Does this answer your question? Case insensitive 'Contains(string)'
  • @ThomasHeijtink, No, this question is different. Here I want to get List of items that matches a keyword. Question you suggested targets on bool whether keyword exists in the List. Both questions target different return types.
  • But the idea is exactly the same. But you have a list of strings. The answer I suggested addresses the situation of a single string. What gives?
  • See my comments on the answer by Javier Silva Ortíz - I believe this may have problems. I suspect the OP wants the ItemSource to be the query rather than a single value - this is the source of items for a list, so it's natural for there to be more than one value.
  • There are some cultural quirks that make lower-casing (or upper-casing) a string before a case-sensitive "contains" work differently to just using a culture-sensitive, case-insensitive comparison. I think using IndexOf (as per my answer) is a more reliable approach, but I readily admit I don't have a specific example to hand.
  • @JonSkeet you're totally right, however, the OP didn't mention any of that. Besides, do you think from the post that he/she needs to go into such depths. He/she might have come out with more doubts after reading your approach.
  • Would you expect the OP to specifically say "I want it to work even in corner cases"? That would be a little odd. And yes, I think in general it's better to post code which works even in corner cases. I don't see why you'd think that my approach would cause more "doubts". It's just using IndexOf to find something instead of Contains... that's not particularly strange IMO.
  • @JonSkeet, your code worked in the first go. But however can you explain how? Other answers needed some modifications as I want to get result irrespective of keyword = "mum" OR keyword = "Mum".
  • @JunedKhanMomin: Well this answer expects the lower-case version of the city name to contain the keyword, which it won't do if the keyword is "Mum". My approach just uses a case-insensitive search instead.