Sunday, May 28, 2017

String to multi-lines of max length

Every couple of years I have to split a long string of text into multiple lines of a maximum length. In the latest example I wanted to display long paragraphs of /? help text in a command line utility where each line must be 79 characters maximum length.

I found some of my old code from 14 years ago where I used a complicated loop to split strings, but now it was time to find a better way. I suspected Regex could be used, but I wasn't sure which syntax to use. After lots of tedious web searching I finally found someone who suggested something like the following, which I've tweaked slightly.

string s = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin ..."; (cut)
int maxlen = 60;
MatchCollection mc = Regex.Matches(s, @"(.{1," + (maxlen - 1) + @"})(?:\s|$)");
var lines = mc.Cast<Match>().Select(m => m.Value);
lines.Select(x => new { Len = x.Length, Line = x }).Dump();

The last line dumps the resulting split lines and their lengths in LINQPad like this:


Note though that if any words exceed the maximum split line length then they will be truncated.

Friday, May 19, 2017

Wiki Articles

I have some obsolete Wiki pages that are of technical and historical interest. It's too hard to migrate the articles over here into the blog, so here are some links to them instead:

Azure Table Storage as an RDB
A curious discussion of how I attempted to use Azure Table Storage like a relational database. It basically worked acceptably well by using combination of code generation, async coding conventions and a trick to generate auto-increment keys. However, as the code grew more complex I started to feel progressively more like I was abusing the technology and I eventually dropped the idea.

Database History and Design
Many years ago I had a popular page in my personal web site that described how I designed and normalized the SQL Server database tables for the Nancy Street collections database. The original page has been partly reproduced and modernised to describe the design of the most recently used database.
Migrating away from SQL Server
In early 2018 I abandoned using SQL Server for my collections database. By migrating to Cosmos DB I underwent an epiphany about how many different database choices we have available now. Is your data best represented as simple tables, relational tables, documents, a hierarchy or a free-form node graph? There is something for you!