Wrapping on Specific Characters

Recently on a project I was working I needed to wrap on backslash characters (file and registry paths). Unfortunately the only help StringFormat can provide is intelligent trimming (e.g. C:\Program Files\….\Foo) and not wrapping options.

I wasn’t so keen on writing my own text drawing/wrapping algorithm, but how to wrap at a specific character? One of my co-workers gave me the spark I needed to figure the whole thing out: use spaces. Now, that is a hack that is visible to the user, and is plain nasty (C:\ Foo\ Bar just doesn’t cut it) – but we live in a unicode world; and unicode has some very interesting characters. The one that helps in this situation is ZERO WIDTH SPACE (U + 200B).

How do we use it? Simply do the following:

Wrap after backslashes
 string wrappable = unwrappable.Replace("\\", "\\\u200B");

That will allow wraps after backslashes. I am sure you could find non-wrap counterparts for most wrap characters as well (e.g. NON BREAK SPACE). I started writing a util class to allow you to wrap before/after any character (that doesn’t usually wrap) or not wrap before/after a character (that usually wraps) - but it was decidedly boring; so I never finished it.

This entry was posted in C#, Migrated, Programming, Top Hacks. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. 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>