My UnWordify Extension Method

Tim Barcz is looking for a solution to an interesting problem in his Coding Contest: Create a Programming Pearl post.

I decided to challenge myself by writing my solution as a single “line” of code:

public static class StringExtensions
{
    private static Dictionary<char, char> _charMap = new Dictionary<char, char>
    {
        { (char)8208,  (char)45 },
        { (char)8211,  (char)45 },
        { (char)8212,  (char)45 },
        { (char)8722,  (char)45 },
        { (char)173,   (char)45 },
        { (char)8209,  (char)45 },
        { (char)8259,  (char)45 },
        { (char)96,    (char)39 },
        { (char)8216,  (char)39 },
        { (char)8217,  (char)39 },
        { (char)8242,  (char)39 },
        { (char)769,   (char)39 },
        { (char)768,   (char)39 },
        { (char)8220,  (char)34 },
        { (char)8221,  (char)34 },
        { (char)8243,  (char)34 },
        { (char)12291, (char)34 },
        { (char)160,   (char)32 },
        { (char)8195,  (char)32 },
        { (char)8194,  (char)32 }
    };

    public static string UnWordify(this string value)
    {
        return new string((from c in value
                           select _charMap.ContainsKey(c) ? _charMap[c] : c)
                          .ToArray());
    }
}

OK, so I cheated and used a dictionary to create my map from the bad characters to the good ones, but the actual method is a single statement.

One thought on “My UnWordify Extension Method”

  1. That’s along the lines (or should I say ‘line’) I was thinking of, now I have to come up with something different! Well done though.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>