When a space isn’t a space

I ran into a scenario recently where this test failed:

[Test]
public void You_have_to_be_kidding_me()
{
    string a = "You have to be kidding me       ";
    string b = "You have to be kidding me       ";

    a.ShouldEqual(b);
}

It took me quite a while to determine the problem, as it originally came from a column in the database with natural keys.

When a test like this fails, it’s time to call it a day.  Any guesses as to the problem?

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Jimmy Bogard

I'm a technical architect with Headspring in Austin, TX. I focus on DDD, distributed systems, and any other acronym-centric design/architecture/methodology. I created AutoMapper and am a co-author of the ASP.NET MVC in Action books.
This entry was posted in C#. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Guesser

    Tab vs space?

  • http://www.peterRitchie.com/blog Peter Ritchie

    No-break space? I love that one… use ALT-0160 (where 0160 are the numbers typed on the numeric keypad) to generated a no-break space.

  • Matt

    Natural keys? ;)

  • Jason L

    Along the lines of what Peter Ritchie said, I would take a look at each char in the string and see where they differ.

    Maybe a :

    for (int i = 0; i < a.Length; i++)
    Assert.AreEqual(a[i], b[i]);

    You could also output each chars int value and look it up on the ascii table (if they are ascii strings).

    and see where they differ.

  • http://terrbear.org terry

    ran into something similar, but instead of spaces it was a hyphen. different encodings from different sources yield unhappy results. here’s a snippet from our codebase:

    #IE blows up on the ‘`’ character. Asciify won’t recognize it as a non-ascii character.
    #Some of these characters look similar, but one is utf-8 and the other is ascii. If
    #you change the encoding of this file (it’s utf-8), I’m pretty sure you just broke the build. don’t do that
    replaces = {‘`’ => ‘\”, ‘–’ => ‘-’, ‘’’ => ‘\”}

    yeah, that’s right, it looks like “map – to -”

    oh, and my guess is you’ve got whitespace in an encoding that doesn’t match the regular \w

  • http://Bryan.ReynoldsLive.com Bryan Reynolds

    Finally, real proof of magic! I have waited so long for this.

  • http://jimmybogard.lostechies.com Jimmy Bogard

    @Peter

    DING DING DING DING

    You got it.

    I love Unicode…and I have zero idea how that value got in there in the first place.

  • http://jagregory.com James Gregory

    I had a similar problem recently where I was stripping newlines from some imported data, except the data had a combination of newlines and vertical tabs! Fun times.