I am using linq to search through a list (user enters query in a textbox).
I want this to be case-insensitive and tried to use IgnoreCase, but I have no idea where to put it.... I know I could use upper or lower, but I would like to hear if anybody has any alternative methods? What would be considered best practise? Regex did not seem to work either?
string searchQuery = tbSearchQuery.Text;
var something= from x in y
where x.Subject.Contains(searchQuery)
select x;
The best way to do a case insensitive comparison in JavaScript is to use RegExp match() method with the i flag.
To do a case-insensitive comparison, use the ILIKE keyword; e.g., column ILIKE 'aBc' and column ILIKE 'ABC' both return TRUE for 'abc' . In contrast, MySQL and MS SQL Server have case-insensitive behaviors by default. This means WHERE column = 'abc' returns TRUE for e.g., 'abc' , 'ABC' , or 'aBc' .
Using the casefold() method is the strongest and the most aggressive approach to string comparison in Python. It's similar to lower() , but it removes all case distinctions in strings. This is a more efficient way to make case-insensitive comparisons in Python.
operators differs from string comparison using the String. CompareTo and Compare(String, String) methods. They all perform a case-sensitive comparison.
Since nobody else has put it up yet, I'd propose using the static String.Equals so you don't have to worry about null
and get back only the information you want.
String.Compare also works but you're not trying to sort the strings (the reason for the integer return value), just determine whether they are value-equal under case-insensitive comparison.
var something = from x in y
where string.Equals(x.Subject, searchQuery, StringComparison.CurrentCultureIgnoreCase)
select x;
string searchQuery = tbSearchQuery.Text;
var something= from x in y
where x.Subject.IndexOf(searchQuery, StringComparison.OrdinalIgnoreCase) >= 0
select x;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With