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

asked Mar 21, 2013 at 7:08
0

5 Answers 5

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.

answered Mar 21, 2013 at 7:10
Sign up to request clarification or add additional context in comments.

6 Comments

That will be somewhat culture-insensitive, however. We don't know that's the required type of comparison.
@JonSkeet what is culture-insensitive? is it something related to globalization support?
@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.
here, whats the diff between .ToUpperVariant() and ToUpper(CultureInfo) ?
@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.
|
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
}
answered Mar 21, 2013 at 7:10

Comments

2
string1.ToUpper().Contains(string2.ToUpper())
answered Mar 21, 2013 at 7:10

2 Comments

@FrédéricHamidi My haste to answer reversed my logic
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.

answered Mar 21, 2013 at 7:12

Comments

1

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

or use StringComparison.OrdinalIgnoreCase

click here for more info

answered Mar 21, 2013 at 7:11

3 Comments

There is no such argument. -- msdn.microsoft.com/en-us/library/dy85x1sa.aspx
string.Contains doesn't have an overload which takes StringComparison.OrdinalIgnoreCase
my mistake copied same text

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.