Adi Drumea

Wednesday, September 02, 2009

[.NET] String.IndexOf

From MSDN: This method performs a word (case-sensitive and culture-sensitive) search using the current culture.

I knew it was case-sensitive, but I had to solve a bug to discover it was also culture-sensitive. This lead to some annoying stuff like searching for string "Țară" ("country" in Romanian) in string "Prima oară" ("first time" in Romanian) would return index = 7. Because the default culture was en-US, IndexOf looked at "Ț" as empty string, so it would match just "ară". Weird default behavior if you ask me.

Anyway, they have decided to change it for .NET 4.0: "Please note that on Silverlight, and starting in .NET Framework 4.0, this method performs an ordinal comparison instead of a culture-sensitive comparison using the current culture (CultureInfo.CurrentCulture). This will result in this method having a different behavior on these platforms. Instead, it is advised that the String.IndexOf(String, StringComparison) overload be used on all platforms to minimize the impact to your existing applications."