Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

special symbols in .net and IndexOf

Tags:

I found an interesting bug, maybe even in .net (haven't try this in mono yet).

IndexOf() method of string instance is returning signed values (-1 or lower) for certain special symbols,

for example I had a string which contained some special unicode characters and somewhere inside of this string was colon which I was looking for. Calling IndexOf(" :") for a line that surely contains " :" returned signed value

I will try to paste this string here, but given the special symbols it may be hard:

hitchcock.freenode.net 322 petan #hobbiton 5 :ˁ˚ᴥ˚ˀ > Good luck axa!

Is there a way to work around this?

like image 299
Petr Avatar asked Jul 10 '13 14:07

Petr


1 Answers

This is documented on the BCL Blog

IndexOf() does a culture invariant comparison by default.

Note this in particular:

UPDATE for .NET 4 Beta 1 In order to maintain high compatibility between .NET 4 and previous releases, we have decided to revert this change. The behavior of String's default partial matching overloads and String and Char's ToUpper and ToLower methods now behave the same as they did in .NET 2.0/3.0/3.5. The change back to the original behavior is present in .NET 4 Beta 1. We apologize for any interim confusion this may cause. We continue to recommend being explicit about the string comparison behavior you want, by always specifying a StringComparison value for the methods on String that accept it.

You should use the String.IndexOf Method (String, Int32, StringComparison) overload:

For example:

IndexOf(":", StringComparison.Ordinal);
like image 109
DGibbs Avatar answered Nov 02 '22 16:11

DGibbs