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:
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.
posted @ Thursday, April 23, 2009 1:00 AM