-2

How can I test whether a substring is contained in other, but case-insensitively?

string1.Contains(string2) is not case insensitive. if there is no existing method, I would want to write a optimized method, any pointer for that?

Edit: It should also work with globalization.

Ankit
  • 6,554
  • 6
  • 49
  • 71

5 Answers5

4

You can convert both strings to upper case before performing the check:

string1.ToUpperInvariant().Contains(string2.ToUpperInvariant())

Or if you want to take the current culture into account when defining case insesitivity:

string1.ToUpper().Contains(string2.ToUpper())

Or you could even use a specific culture by calling the ToUpper overload that accepts a CultureInfo.

The reason that you should convert to upper case and not lower case is described in code analysis warning CA1308: Normalize strings to uppercase:

Strings should be normalized to uppercase. A small group of characters, when they are converted to lowercase, cannot make a round trip. To make a round trip means to convert the characters from one locale to another locale that represents character data differently, and then to accurately retrieve the original characters from the converted characters.

You may wonder what "small group of characters" are affected by this and apparently at least some Georgian scripts have this problem according to Michael S. Kaplan.

Community
  • 1
  • 1
Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
  • 3
    That will be somewhat culture-insensitive, however. We don't know that's the required type of comparison. – Jon Skeet Mar 21 '13 at 07:13
  • @JonSkeet what is culture-insensitive? is it something related to globalization support? – Ankit Mar 21 '13 at 07:24
  • 2
    @ay89: Yes. Different strings will be "equal" in a case-insensitive way to different people, based on their culture. Look up the "Turkey test" for particularly tricky examples. – Jon Skeet Mar 21 '13 at 07:26
  • here, whats the diff between .ToUpperVariant() and ToUpper(CultureInfo) ? – Ankit Mar 21 '13 at 07:31
  • Since I'm from _Turkey_ this is an example http://en.wikipedia.org/wiki/Dotted_and_dotless_I – Soner Gönül Mar 21 '13 at 07:38
  • 2
    @ay89: `ToUpperInvariant` converts to upper case using the _invariant culture_ (`CultureInfo.InvariantCulture`) which is based on the "en-US" culture. Upper casing the letter __i__ will result in __I__. Using `ToUpper(CultureInfo.GetCultureInfo("tr-TR"))` will result in __İ__ (Turkish upper case dotted I) as pointed out by Soner Gönül. – Martin Liversage Mar 21 '13 at 07:53
4

Use String.IndexOf Method (String, StringComparison)

string str = "Some test String";
string str2 = "test";
if (str.IndexOf(str2, StringComparison.InvariantCultureIgnoreCase) > 0)
{
    //str contains str2
}
else
{
    //str does not contain str2
}
Habib
  • 219,104
  • 29
  • 407
  • 436
2
string1.ToUpper().Contains(string2.ToUpper())
jordanhill123
  • 4,142
  • 2
  • 31
  • 40
2

You can use this overload of String.IndexOf Method (String, StringComparison)

For StringComparison.InvariantCultureIgnoreCase

Compare strings using culture-sensitive sort rules, the invariant culture, and ignoring the case of the strings being compared.

string string1 = "YOURSTRING";
bool containsornot = string1 .IndexOf("yourstring", StringComparison.InvariantCultureIgnoreCase) >= 0;

Here is a DEMO.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
1

have you tried string1.IndexOf(string2, StringComparison.InvariantCultureIgnoreCase)

or use StringComparison.OrdinalIgnoreCase

click here for more info

Satpal
  • 132,252
  • 13
  • 159
  • 168