When a space isn’t a space

I ran into a scenario recently where this test failed:

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


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?

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?

  • 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.

  • 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

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

  • @Peter


    You got it.

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

  • 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.