Falcon Falcon - 1 month ago 6
C# Question

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


Possible Duplicate:

Case insensitive contains(string)




With
Contains()
method of String class a substring can be found.
How to find a substring in a string in a case-insensitive manner?

Answer

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.

Comments