Sunday, May 1, 2011

Whats the difference between these two strings?

Using nUnit to test the output (currency formatting) for a specific culture but we are getting the following result.

  Fail: Formatting currency amount
  String lengths are both 11. Strings differ at index 2.
  Expected: "12 765,87 €"
  But was:  "12 765,87 €"
  -------------^

We can't see the difference between the strings. Our expected result uses a "Space" character.

Is there a different space character that we can put into the accepted result to get the test to pass?

By the way, the tested culture is fr-FR.

Edit: Thanks Adam you are spot on with the unicode character. We have changed our expected results and now each Unit test passes.

From stackoverflow
  • Can you post the ascii code of the char at index two in both strings?

  • It's possible they're different types of spaces. Unicode has a lot of different space characters. Take a look at the code points at index 2 by casting the characters to integers to get your answer.

    Edit

    In response to your comment, code point 160 is a non-breaking space. You can enter it directly into your source code (e.g. Alt+0160 on the numeric keypad on Windows), or use an escape sequence:

    // U+20AC is the Unicode code point for the euro sign
    string expected = "12\u00A0765,87 \u20AC";
    
  • Most likely it's a character that is blank, but not a space, to avoid labels and such to break the number in two when word-wrapping is enabled.

0 comments:

Post a Comment