20

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?

4

2 回答 2

10

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);
于 2013-07-10T14:53:13.100 回答
9

I see that some people want to close this question for some reason, so I will answer it before that happens :-)

Thanks to answers in comments by @vcsjones it seems to be related to locale settings, and can be fixed by changing

text.IndexOf(" :")

to

text.IndexOf(" :", StringComparison.Ordinal)

This may be a poor answer, but better than nothing...

于 2013-07-10T14:42:34.683 回答