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


can suggest method writing mutli-line string system console , having text block indented? i'm looking relatively lightweight because it's being used displaying command line program.

note: approach described below not meet updated requirements described @billman in question's comments. will not automatically wrap lines longer console line length - use approach if wrapping isn't issue.


simple option, use string.replaceall() follows:

string output = <your string here> string indented = output.replaceall("(?m)^", "\t"); 

if you're unfamiliar java regular expressions, works follows:

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

as example, following code:

string output = "foo\nbar\nbaz\n" string indented = output.replaceall("(?m)^", "\t"); system.out.println(indented); 

produces output:

 	foo 	bar 	baz 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -