fqpnla
Last Updated: February 25, 2016
·
759
· mortimerpa
4f581c1a7bc4d712fb4f4a51139ed1f8

WordWrap with Scala

This is a simple issue: take a String and add line breaks after a certain amount of "fill", usually this is to make the string display nicer for humans, cutting at around 78 characters.

Langref has a few solutions, but they cut words, so you end up with something like:

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over t
he lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox"

Wouldn't it be nicer if you could cut it around word delimiters?

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the
lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox"

A simple enough solution is to use a regexp:

val wrapRegEx = """(.{1,78})\s""".r
def wrapLine(s: String) = wrapRegEx.replaceAllIn(s, m=>m.group(1)+"\n")
Say Thanks
Respond