Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to indent a multi-line paragraph being written to the console in java

Tags:

java

console

Can anyone suggest a method for writing a mutli-line String to a system console and having that text block be indented? I'm looking for something relatively lightweight because it's only being used for displaying help for a command line program.

like image 822
BillMan Avatar asked Apr 08 '13 21:04

BillMan


People also ask

How do you indent multiple lines?

If you prefer using [spacebar] to indent your code rather than using [tab], you can select multiple lines by holding the [alt] key and clicking on the beginning of each line you want to indent. Then, you can press [spacebar] and all the selected lines will be affected. omg it moves!

How do you write a multi line statement in Java?

Displaying Multiple Lines of Text with a Single Statement. A single statement can display multiple lines by using newline characters, which indicate to System. out's print and println methods when to position the output cursor at the beginning of the next line in the command window.


2 Answers

NOTE: The approach described below does not meet the updated requirements described by @BillMan in the question's comments. This will not automatically wrap lines that are longer than the console line length - only use this approach if wrapping isn't an issue.


As a simple option, you could use String.replaceAll() as follows:
String output = <your string here>
String indented = output.replaceAll("(?m)^", "\t");

If you're unfamiliar with Java regular expressions, it works as follows:

  • (?m) enables multiline mode. This means each line in output is considered individually, instead of treating output as a single line (which is the default).
  • ^ is a regex matching the start of each line.
  • \t causes each match of the preceding regex (i.e. the start of each line) to be replaced by a tab character.

As an example, the following code:

String output = "foo\nbar\nbaz\n"
String indented = output.replaceAll("(?m)^", "\t");
System.out.println(indented);

Produces this output:

	foo
	bar
	baz
like image 133
Mac Avatar answered Oct 17 '22 03:10

Mac


With JDK/12 early access builds, one can now make use of the indent API of the String class which is currently available under the preview feature and can be used as :

String indentedBody =
`<html>
   <body>
       <p>Hello World - Indented.</p>
   </body>
</html>`.indent(4);

and the output of the above code would be

    <html>
       <body>
           <p>Hello World - Indented.</p>
       </body>
    </html>

The current documented specification of the API further is as follows:

/**
 * Adjusts the indentation of each line of this string based on the value of
 * {@code n}, and normalizes line termination characters.
 * <p>
 * This string is conceptually separated into lines using
 * {@link String#lines()}. Each line is then adjusted as described below
 * and then suffixed with a line feed {@code "\n"} (U+000A). The resulting
 * lines are then concatenated and returned.
 * <p>
 * If {@code n > 0} then {@code n} spaces (U+0020) are inserted at the
 * beginning of each line. {@link String#isBlank() Blank lines} are
 * unaffected.
 * <p>
 * If {@code n < 0} then up to {@code n}
 * {@link Character#isWhitespace(int) white space characters} are removed
 * from the beginning of each line. If a given line does not contain
 * sufficient white space then all leading
 * {@link Character#isWhitespace(int) white space characters} are removed.
 * Each white space character is treated as a single character. In
 * particular, the tab character {@code "\t"} (U+0009) is considered a
 * single character; it is not expanded.
 * <p>
 * If {@code n == 0} then the line remains unchanged. However, line
 * terminators are still normalized.
 * <p>
 *
 * @param n  number of leading
 *           {@link Character#isWhitespace(int) white space characters}
 *           to add or remove
 *
 * @return string with indentation adjusted and line endings normalized
 *
 * @see String#lines()
 * @see String#isBlank()
 * @see Character#isWhitespace(int)
 *
 * @since 12
 */
public String indent(int n)
like image 5
Naman Avatar answered Oct 17 '22 04:10

Naman