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")
Written by Pierre Andrews
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Scala
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#