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,
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)); 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)); YOU CAN use Contains by providing an indifferent string equality match in this way:
if (myList.Contains(keyword, StringComparer.OrdinalIgnoreCase)) { Console.WriteLine("Keyword Exists"); } The best solution is to ignore the case when comparing
List<string> searchResults = sl.FindAll(s => s.IndexOf(seachKeyword, System.StringComparison.OrdinalIgnoreCase) >= 0); StringComparer.CurrentCultureIgnoreCase is a better approach instead of using indexOf. 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 () ));
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); 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;
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; }
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