Generic pretty print based on chars per line


/ Published in: Java
Save to your folder(s)

Generic method for printing a list to the console based on the number of characters per line.


Copy this code and paste it in your HTML
  1. private <T extends Object> String prettyPrintCharsPerLine(T item, int numberOfCharsPerLine) {
  2. // initialize a mutable string
  3. StringBuffer pretty = new StringBuffer(item.toString());
  4.  
  5. // now insert a line break at every point necessary
  6. // the break comes after the numberOfCharsPerLine
  7. int pos = numberOfCharsPerLine;
  8. while (pos < pretty.length()) {
  9. pretty.insert(pos+1, '\n');
  10. pos += numberOfCharsPerLine;
  11. }
  12.  
  13. return pretty.toString();
  14. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.