none
Seems a bug of string.IndexOf() RRS feed

  • Question

  • Hello there!

    Please look at this Immediate Window log:

    1"'>".IndexOf("'>"); 
    2
    3"'>゛".IndexOf("'>"); 
    4-1

    Please take care on the 4th character of the 3rd line, if I remove it, IndexOf returns 0, if I keep it there, IndexOf will return -1.

    Any way to work around this?


    Thanks!

    Do my best in .Net learning.
    Monday, February 2, 2009 8:08 PM

Answers

  • You can specify an ordinal comparison to do a direct comparison of the Unicode character numbers, bypassing any special rules as to what characters are considered equal:

     
    Console.WriteLine("'>゛".IndexOf("'>", StringComparison.Ordinal));   
     
     

    As to why this is happening, it probably has to do with the fact that Char.GetUnicodeCategory returns "ModifierSymbol" for the character in question.  Thus, it is interpreted as modifying the preceding >, making it different than the > in the other test case.

    • Marked as answer by a_a Monday, February 2, 2009 10:00 PM
    Monday, February 2, 2009 8:19 PM

All replies

  • You can specify an ordinal comparison to do a direct comparison of the Unicode character numbers, bypassing any special rules as to what characters are considered equal:

     
    Console.WriteLine("'>゛".IndexOf("'>", StringComparison.Ordinal));   
     
     

    As to why this is happening, it probably has to do with the fact that Char.GetUnicodeCategory returns "ModifierSymbol" for the character in question.  Thus, it is interpreted as modifying the preceding >, making it different than the > in the other test case.

    • Marked as answer by a_a Monday, February 2, 2009 10:00 PM
    Monday, February 2, 2009 8:19 PM
  • Hello!

    Your answer is working for me! Thank you! :D

    Do my best in .Net learning.
    Monday, February 2, 2009 10:02 PM