Case-Insensitive List Search

Case-Insensitive List Search

Instead of String.IndexOf, use String.Equals to ensure you don't have partial matches. Also don't use FindAll as that goes through every element, use FindIndex (it stops on the first one it hits).

if(testList.FindIndex(x => x.Equals(keyword,  
StringComparison.OrdinalIgnoreCase) ) != -1)
Console.WriteLine("Found in list");

Alternately use some LINQ methods (which also stops on the first one it hits)

if( testList.Any( s => s.Equals(keyword, StringComparison.OrdinalIgnoreCase) ) )
Console.WriteLine("found in list");

How to make a case insensitive list search in C#?

You could use IndexOf with a case-insensitive comparison:

var query = posts.Where(
logg => logg.IndexOf(searchKey, StringComparison.CurrentCultureIgnoreCase) != -1);
foreach (string result in query)
{
Console.WriteLine("\n{0}", result);
}

Note that while I'd expect this to work for IEnumerable<T>, I suspect that many IQueryable<T> providers (e.g. EF) may well not support this operation.

How to ignore the case sensitivity in Liststring

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));

How do I get the case insensitive match in Liststring?

You want something like:

string match = list.FirstOrDefault(element => element.Equals(target, 
StringComparison.CurrentCultureIgnoreCase));

This will leave match as a null reference if no match can be found.

(You could use List<T>.Find, but using FirstOrDefault makes the code more general, as it will work - with a using System.Linq; directive at the top of the file) on any sequence of strings.)

Note that I'm assuming there are no null elements in the list. If you want to handle that, you may want to use a static method call instead: string.Equals(element, target, StringComparison.CurrentCultureIgnoreCase).

Also note that I'm assuming you want a culture-sensitive comparison. See StringComparison for other options.

Case insensitive list filter

You can use str.casefold.

filter_object = list(filter(lambda a: f_val.casefold() in a.casefold(), f_Results))

Option to ignore case with .contains method?

I'm guessing you mean ignoring case when searching in a string?

I don't know any, but you could try to convert the string to search into either to lower or to upper case, then search.

// s is the String to search into, and seq the sequence you are searching for.
bool doesContain = s.toLowerCase().contains(seq);

Edit:
As Ryan Schipper suggested, you can also (and probably would be better off) do seq.toLowerCase(), depending on your situation.

How do I make a list comparison be case insensitive?

The example you've given seems to work, but I'm going to assume that current_ids may have a mixture of upper/lower case.

What you could do is convert every string in current_ids to lower case by using a list comprehension:

current_ids = [i.lower() for i in current_ids]

This creates a new list of every word in current_ids all lower case.

Then, you can compare as you are doing now (if new.lower() in current_ids)

Comparing items of case-insensitive list

fruit.lower() in the for loop won't work as the error message implies, you can't assign to a function call..

What you could do is create an auxiliary structure (set here) that holds the lowercase items of the existing fruits in fruits, and, append to fruits if a fruit.lower() in fruit_add isn't in the t set (containing the lowercase fruits from fruits):

t = {i.lower() for i in fruits}
for fruit in fruits_add:
if fruit.lower() not in t:
fruits.append(fruit)

With fruits now being:

print(fruits)
['Apple', 'banana', 'Kiwi', 'melon', 'strawberry']


Related Topics



Leave a reply



Submit