sl = new List(); sl.Add("stor...">

How to ignore case sensitivity in List <string>

Say I have this code

string seachKeyword = ""; List<string> sl = new List<string>(); sl.Add("store"); sl.Add("State"); sl.Add("STAMP"); sl.Add("Crawl"); sl.Add("Crow"); List<string> searchResults = sl.FindAll(s => s.Contains(seachKeyword)); 

How can I ignore case of letters in the search for Contains?

Thank,

+47
c #
Jun 24 '10 at 6:49
source share
10 answers

Using case-insensitive comparisons is a better option, but the Contains method does not support it.

For this you can use the following:

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

It is better to wrap this in an extension method, for example:

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

So you can use:

 sl.FindAll(s => s.Contains(searchKeyword, StringComparison.OrdinalIgnoreCase)); 
+59
Jun 24 '10 at 7:00
source share

Use Linq, this adds a new .Compare method

 using System.Linq; using System.Collections.Generic; List<string> MyList = new List<string>(); MyList.Add(...) if (MyList.Contains(TestString, StringComparer.CurrentCultureIgnoreCase)) { //found } 

so supposedly

 using System.Linq; ... List<string> searchResults = sl.FindAll(s => s.Contains(seachKeyword, StringComparer.CurrentCultureIgnoreCase)); 
+64
Feb 25 '11 at 10:40
source share

YOU CAN use Contains by providing an indifferent string equality match in this way:

 if (myList.Contains(keyword, StringComparer.OrdinalIgnoreCase)) { Console.WriteLine("Keyword Exists"); } 
+10
Sep 14 '16 at 11:06
source share

The best solution is to ignore the case when comparing

 List<string> searchResults = sl.FindAll(s => s.IndexOf(seachKeyword, System.StringComparison.OrdinalIgnoreCase) >= 0); 
+6
Jun 24. '10 at 7:00
source share
 StringComparer.CurrentCultureIgnoreCase is a better approach instead of using indexOf. 
+5
Jul 09 '14 at 5:12
source share

You can apply a little trick to this.
Change the whole line to the same case : either upper or lower case

Search listResults = sl.FindAll (s => s.ToUpper () . States ( seachKeyword.ToUpper () ));

0
Jul 27 '15 at 7:47
source share

For those of you who are having trouble finding through the LIST OF SHEETS, I have found a solution.

In this example, I searched for the Jagged list and grabbed only the lists for which the first line matches the argument.

 List<List<string>> TEMPList = new List<List<string>>(); TEMPList = JaggedList.FindAll(str => str[0].ToLower().Contains(arg.ToLower())); DoSomething(TEMPList); 
0
Mar 03 '16 at 21:48
source share

FindAll enumerates the entire list.

a better approach would be to smash it after finding the first instance.

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

0
Mar 24 '16 at 19:57
source share

Below, the method will search for the required keyword and add all the elements found to the new list and then return the 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; }

0
Sep 20 '17 at 5:10
source share

One of the possible (maybe not the best) is the string line of all the lines placed in sl. Then you enter a lowercase search key.

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

-one
Jun 24 '10 at 6:53
source share



All Articles