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 inoutputconsidered individually, instead of treatingoutputsingle line (which default).^regex matching start of each line.\tcauses 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
Post a Comment