none
string indexof comparison fail? RRS feed

  • Question

  • Wondering why the second IndexOf fails?

                "abc<hide>.</hide>".IndexOf("<hide>")
                "abc<hide>ʿ.</hide>".IndexOf("<hide>")
                "abc<hide>ʿ.</hide>".IndexOf("<hide>", 0, StringComparison.Ordinal)

    the values returned, respectively are 3, -1, 3.


    David K. Ream

    Saturday, September 30, 2017 3:05 PM

Answers

  • @Dave, what I can see it is not an open apostrophe after <hide> it is an symbol

    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    • Marked as answer by Dave Ream Monday, October 2, 2017 11:17 AM
    Sunday, October 1, 2017 8:15 AM
    Moderator

All replies

  • Hi dave,

    It is because your second <hide> is as <hide>ʿ which is not same.


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Saturday, September 30, 2017 3:13 PM
    Moderator
  • I don't understand the distinction. I all three situation <hide> is followed by another character.

    David K. Ream

    Saturday, September 30, 2017 3:16 PM
  • @Dave, your first <hide> didn't contains any symbol with it and for third case you are comparing using the String as "Ordinal" which means "It indicates that the characters in the string should be treated by their numeric value. The capital letter "A" would always represent the number 65 because of the ASCII layout."

    So, for the third case it when comparing using "Ordinal" it checks the ASCII value which is there even though there is a symbol.

    You could look into this tutorial: A Beginner's Tutorial on String Comparison in C#


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]



    Saturday, September 30, 2017 3:25 PM
    Moderator
  • I read the article you cited and I've been programming in C# for years so I didn't see anything new there.

    The part of your response I am still not grokking is what you mean by "your first <hide> didn't contains any symbol with it". The second example has an open single apostrophe after <hide>. what does that matter to the string being searched for?


    David K. Ream

    Saturday, September 30, 2017 4:51 PM
  • Wondering why the second IndexOf fails?

                "abc<hide>.</hide>".IndexOf("<hide>")
                "abc<hide>ʿ.</hide>".IndexOf("<hide>")
                "abc<hide>ʿ.</hide>".IndexOf("<hide>", 0, StringComparison.Ordinal)

    the values returned, respectively are 3, -1, 3.

    I suspect that it's related to the way string comparisons are done using
    culture-specific rules to determine equality. The special character that
    you have embedded in that string is 0x02BF (U+02BF or dec 703) which is
    the "Modifier Letter Left Half Ring" from the Spacing Modifier Letters
    Unicode block. See:

    https://en.wikipedia.org/wiki/Spacing_Modifier_Letters

    https://en.wikipedia.org/wiki/Unicode_block

    Note that

    int n = ">ʿ".IndexOf(">");

    will fail to match. It returns -1 as well.

    If the extra character were instead a single quote character (0x27)

    int n = ">'".IndexOf(">");

    the match would be found at index 0.

    - Wayne
     
     

    • Proposed as answer by WayneAKing Monday, October 2, 2017 11:23 AM
    Sunday, October 1, 2017 7:57 AM
  • @Dave, what I can see it is not an open apostrophe after <hide> it is an symbol

    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    • Marked as answer by Dave Ream Monday, October 2, 2017 11:17 AM
    Sunday, October 1, 2017 8:15 AM
    Moderator
  • Hi , 

    You need to use comparaison options 
    StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase it will return 3 

    like this
    "abc<hide>ʿ.</hide>".IndexOf("<hide>", StringComparison.Ordinal)

    Useful link :
    https://msdn.microsoft.com/en-us/library/e6883c06.aspx#Examples

    -----------------

    [If my post helps to resolve your issue, please click the "Mark as Answer" of that post or click
     
    Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful,
    you help others find the answer faster. ]





    Monday, October 2, 2017 10:48 AM


  • You need to use comparaison options 
    StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase it will return 3 

    like this
    "abc<hide>ʿ.</hide>".IndexOf("<hide>", StringComparison.Ordinal)


    The OP already knows how to use that to get a correct match. Look at the
    third example in the very first post by the OP - it uses 

    StringComparison.Ordinal

    which returns the correct value of 3. The OP wanted to know why the
    second example returned -1. Your reply adds nothing new and certainly 
    doesn't answer the OP's question as you have proposed. This is another
    example of why people should not propose their own suggestions as the "Answer".

    - Wayne


    • Edited by WayneAKing Monday, October 2, 2017 11:07 AM
    Monday, October 2, 2017 11:06 AM
  • You are right . I didn't read well the post , my bad 
    Monday, October 2, 2017 11:10 AM