Wednesday, June 17, 2009

IndexOf method in strings

The string's IndexOf methods are all case-sensitive.

Fortunately, we have the CompareInfo class in the Globalization namespace, that includes a case-insensitive IndexOf method

using System.Globalization;
string strValue = "C# is a GREAT programming language.";
string strCheck = "great";

CompareInfo Compare = CultureInfo.InvariantCulture.CompareInfo;
int i = Compare.IndexOf( strValue, strCheck, CompareOptions.IgnoreCase );


Compare.IndexOf here returns the index of the string "great".

No comments:

Post a Comment