0

In Java, I want to print a rather long String to the console, but instead of having it printed as one really long line, I'd like to turn it into a nicely formatted paragraph.

So example:

String something = "abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890"

Would print as

abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890

But I would like something like this

abcdefghijklmnopqrstuvwxyz1234567890
abcdefghijklmnopqrstuvwxyz1234567890

How to make new lines where appropriate without having to do it manually with \n?

  • 3
    Define _appropriate_. – Sotirios Delimanolis Jun 22 '14 at 23:38
  • 3
    This question has the appropriate answer: [http://stackoverflow.com/questions/4212675/wrap-the-string-after-a-number-of-character-word-wise-in-java][1] [1]: http://stackoverflow.com/questions/4212675/wrap-the-string-after-a-number-of-character-word-wise-in-java – Frement Jun 22 '14 at 23:40
  • What's wrong with `\n`? – BitNinja Jun 22 '14 at 23:48
  • Appropriate could be at every period "." or after a certain number of characters. Nothing is wrong with \n, but I don't want to place them manually in my very long strings. – user3765722 Jun 23 '14 at 00:00

1 Answers1

0

use this

String msg="abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890";
String h[]=msg.split("0", 2);
System.out.println(h[0]+"0"+"\n"+h[1]);

or use this

  String h[]=msg.split("0", 2);
  System.out.println(h[0]+"0");
  System.out.println(h[1]);
harish
  • 41
  • 2
  • 1
    This works only for the given example, the question is more generic. OP wants an answer that’d work for *any* long string. – bfontaine Aug 25 '14 at 12:03