3

I want to format a String, ready for output to a terminal window. The terminal window itself does not automatically wrap long lines, so I want to insert line breaks into the String.

Is there a way to achieve this using the String.format() method?

EDIT: Just to clarify. I do not want to hard code the line-breaks into the String.

I want to go from an Input like this:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

To an output like this:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor \n incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud \n exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure \n dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\n Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt \n mollit anim id est laborum.

(Emphasis for readability)

diestl
  • 2,020
  • 4
  • 23
  • 37

4 Answers4

3

I would suggest you use an existing library, such as Apache Commons Lang WordUtils.wrap() rather than writing something yourself using String.format().

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
3

There's no way to insert line breaks with String.format, but you can use a regular expression to do it. For example this splits a string into lines of 50 characters max by whitespace:

str += "\n"; // Needed to handle last line correctly
str = str.replaceAll("(.{1,50})\\s+", "$1\n");

It's not perfect though; "words" longer than the maximum wont get split for example.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • Accepted. I ended up using the WordUtils.wrap() method, but this come closest to what I asked for in the question. – diestl Aug 21 '13 at 12:57
  • The `java.text.BreakIterator` class would give better results--for example it knows that it's OK to break after a hyphen--but using it requires iterating over the string in a loop http://docs.oracle.com/javase/tutorial/i18n/text/line.html – Joni Aug 21 '13 at 14:09
2

I think I have a solution for you : you can split your line using a regular expression to return a fixed length array of Strings. Here is the expression :

final String[] fixedLengthLines = line.split("(?<=\\G.{" + lineLength + "})");

Then you just have to output your array of Strings appending \n whenever it's needed.

This will work only if your output terminal displays fixed size characters of course :)

Note : it comes from here : Split string to equal length substrings in Java

Community
  • 1
  • 1
Julien
  • 2,544
  • 1
  • 20
  • 25
0

If I understood the question correctly you need to use %n platform specific line separator in format pattern.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275