Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore case when comparing string?

Tags:

c#

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;
like image 371
Iris Classon Avatar asked Oct 18 '11 08:10

Iris Classon


People also ask

How do you compare strings in regardless of case?

The best way to do a case insensitive comparison in JavaScript is to use RegExp match() method with the i flag.

How do I compare strings in SQL case-insensitive?

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' .

How do you ignore a case of strings in Python?

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.

Is comparing strings case-sensitive?

operators differs from string comparison using the String. CompareTo and Compare(String, String) methods. They all perform a case-sensitive comparison.


2 Answers

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;
like image 169
mlorbetske Avatar answered Oct 13 '22 01:10

mlorbetske


string searchQuery = tbSearchQuery.Text; 
var something= from x in y  
               where x.Subject.IndexOf(searchQuery, StringComparison.OrdinalIgnoreCase) >= 0
               select x; 
like image 40
Dylan Smith Avatar answered Oct 13 '22 01:10

Dylan Smith