c# - Case insensitive 'Contains(string)'


Is there a way to make the following return true?

string title = "ASTRINGTOTEST";
title.Contains("string");

There doesn't seem to be an overload that allows me to set the case sensitivity.. Currently I UPPERCASE them both, but that's just silly.

UPDATE
The sillyness I refer to is the i18n issues that come with up- and down casing.

UPDATE
This question is ancient and since then I have realized I asked for a simple answer for a really vast and difficult topic if you care to investigate it fully.
For most cases, in mono-lingual, English code bases this answer will suffice. I'm suspecting because most people coming here fall in this category this is the most popular answer.
This answer however brings up the inherent problem that we can't compare text case insensitive until we know both texts are the same culture and we know what that culture is. This is maybe a less popular answer, but I think it is more correct and that's why I marked it as such.


Answers


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'll be wrong in familiar ways.




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

...

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



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.




How to make String.Contains case insensitive?

You can create your own extension method to do this:

public static bool Contains(this string source, string toCheck, StringComparison comp)
  {
    return source != null && toCheck != null && source.IndexOf(toCheck, comp) >= 0;
  }

And then call:

 mystring.Contains(myStringToCheck, StringComparison.OrdinalIgnoreCase);



You can use:

if (myString1.IndexOf("AbC", StringComparison.OrdinalIgnoreCase) >=0) {
    //...
}

This works with any .NET version.




bool b = list.Contains("Hello", StringComparer.CurrentCultureIgnoreCase);

[EDIT] extension code:

public static bool Contains(this string source, string cont
                                                    , StringComparison compare)
{
    return source.IndexOf(cont, compare) >= 0;
}

This could work :)




Find a substring in a case-insensitive way - C#

You can use the IndexOf() method, which takes in a StringComparison type:

string s = "foobarbaz";
int index = s.IndexOf("BAR", StringComparison.CurrentCultureIgnoreCase); // index = 3

If the string was not found, IndexOf() returns -1.




There's no case insensitive version. Use index of instead (or a regex).

string string1 = "my string";
string string2 = "string";
bool isContained = string1.IndexOf(string2, StringComparison.OrdinalIgnoreCase) >= 0;

StringComparison.OrdinalIgnoreCase is generally used for more "programmatic" text like paths or constants that you might have generated and is the fastest means of string comparison. For text strings that are linguistic use StringComparison.CurrentCultureIgnoreCase or StringComparison.InvariantCultureIgnoreCase.




Contains returns a boolean if a match is found. If you want to search case-insensitive, you can make the source string and the string to match both upper case or lower case before matching.

Example:

if(sourceString.ToUpper().Contains(stringToFind.ToUpper()))
{
    // string is found
}



Case Insensitive comparison in C#

string.Equals("this will return true", "ThIs WiLL ReTurN TRue", StringComparison.CurrentCultureIgnoreCase)

or, for contains

if (string1.IndexOf(string2, StringComparison.CurrentCultureIgnoreCase) >= 0)




I prefer an extension method like this.

public static class StringExtensions
{
    public static bool Contains(this string source, string value, StringComparison compareMode)
    {
        if (string.IsNullOrEmpty(source))
            return false;

        return source.IndexOf(value, compareMode) >= 0;
    }
}

Notice that in this way you could avoid the costly transformation in upper or lower case.

You could call the extension using this syntax

 bool result = "This is a try".Contains("TRY", StringComparison.InvariantCultureIgnoreCase);
 Console.WriteLine(result);

Please note: the above extension (as true for every extension method) should be defined inside a non-nested, non-generic static class See MSDN Ref




Why not this:

if (string1.IndexOf(string2, StringComparison.OrdinalIgnoreCase) >= 0)
{  
}