private static List<String> wrap(String text,int width){
  List<String> lines=Collections.singletonList(text);
  if (text.length() > width) {
    String[] chunks=text.split("\\s+");
    lines=new ArrayList<>();
    StringBuilder line=new StringBuilder();
    for (    String chunk : chunks) {
      if (line.length() + chunk.length() > width) {
        lines.add(line.toString());
        line.setLength(0);
      }
      if (line.length() != 0) {
        line.append(' ');
      }
      String[] embeddedLines=chunk.split("%n",-2);
      if (embeddedLines.length == 1) {
        line.append(chunk);
      }
 else {
        for (int i=0; i < embeddedLines.length; i++) {
          line.append(embeddedLines[i]);
          if (i < embeddedLines.length - 1) {
            lines.add(line.toString());
            line.setLength(0);
          }
        }
      }
    }
    if (line.length() != 0) {
      lines.add(line.toString());
    }
  }
  return lines;
}

private static List < String > wrap ( String text , int width ) { List < String > lines = Collections . singletonList ( text ) ; if ( text . length ( ) > width ) { String [ ] chunks = text . split ( "\\s+" ) ; lines = new ArrayList < > ( ) ; StringBuilder line = new StringBuilder ( ) ; for ( String chunk : chunks ) { if ( line . length ( ) + chunk . length ( ) > width ) { lines . add ( line . toString ( ) ) ; line . setLength ( 0 ) ; } if ( line . length ( ) != 0 ) { line . append ( ' ' ) ; } String [ ] embeddedLines = chunk . split ( "%n" , - 2 ) ; if ( embeddedLines . length == 1 ) { line . append ( chunk ) ; } else { for ( int i = 0 ; i < embeddedLines . length ; i ++ ) { line . append ( embeddedLines [ i ] ) ; if ( i < embeddedLines . length - 1 ) { lines . add ( line . toString ( ) ) ; line . setLength ( 0 ) ; } } } } if ( line . length ( ) != 0 ) { lines . add ( line . toString ( ) ) ; } } return lines ; }
wraps some given text to one or more lines of a given maximum width .
