c# - Case insensitive 'Contains(string)'

ID : 164

viewed : 347

Tags : c#stringcontainscase-insensitivec#

Top 5 Answer for c# - Case insensitive 'Contains(string)'

vote vote

93

You could use the String.IndexOf Method and pass StringComparison.OrdinalIgnoreCase as the type of search to use:

string title = "STRING"; bool contains = title.IndexOf("string", StringComparison.OrdinalIgnoreCase) >= 0; 

Even better is defining a new extension method for string:

public static class StringExtensions {     public static bool Contains(this string source, string toCheck, StringComparison comp)     {         return source?.IndexOf(toCheck, comp) >= 0;     } } 

Note, that null propagation ?. is available since C# 6.0 (VS 2015), for older versions use

if (source == null) return false; return source.IndexOf(toCheck, comp) >= 0; 

USAGE:

string title = "STRING"; bool contains = title.Contains("string", StringComparison.OrdinalIgnoreCase); 
vote vote

82

To test if the string paragraph contains the string word (thanks @QuarterMeister)

culture.CompareInfo.IndexOf(paragraph, word, CompareOptions.IgnoreCase) >= 0 

Where culture is the instance of CultureInfo describing the language that the text is written in.

This solution is transparent about the definition of case-insensitivity, which is language dependent. For example, the English language uses the characters I and i for the upper and lower case versions of the ninth letter, whereas the Turkish language uses these characters for the eleventh and twelfth letters of its 29 letter-long alphabet. The Turkish upper case version of 'i' is the unfamiliar character 'İ'.

Thus the strings tin and TIN are the same word in English, but different words in Turkish. As I understand, one means 'spirit' and the other is an onomatopoeia word. (Turks, please correct me if I'm wrong, or suggest a better example)

To summarise, you can only answer the question 'are these two strings the same but in different cases' if you know what language the text is in. If you don't know, you'll have to take a punt. Given English's hegemony in software, you should probably resort to CultureInfo.InvariantCulture, because it will be wrong in familiar ways.

vote vote

76

You can use IndexOf() like this:

string title = "STRING";  if (title.IndexOf("string", 0, StringComparison.CurrentCultureIgnoreCase) != -1) {     // The string exists in the original } 

Since 0 (zero) can be an index, you check against -1.

MSDN

The zero-based index position of value if that string is found, or -1 if it is not. If value is String.Empty, the return value is 0.

vote vote

67

Alternative solution using Regex:

bool contains = Regex.IsMatch("StRiNG to search", Regex.Escape("string"), RegexOptions.IgnoreCase); 
vote vote

57

.NET Core 2.0+ (including .NET 5.0+)

.NET Core has had a pair of methods to deal with this since version 2.0 :

  • String.Contains(Char, StringComparison)
  • String.Contains(String, StringComparison)

Example:

"Test".Contains("test", System.StringComparison.CurrentCultureIgnoreCase); 

It is now officially part of the .NET Standard 2.1, and therefore part of all the implementations of the Base Class Library that implement this version of the standard (or a higher one).

Top 3 video Explaining c# - Case insensitive 'Contains(string)'

Related QUESTION?