public static String lineWrap(String input,int maxLineWidth){
  StringBuffer sb=new StringBuffer();
  BreakIterator biterator=BreakIterator.getLineInstance();
  biterator.setText(input);
  int linestart=0;
  int previous=0;
  while (true) {
    int next=biterator.next();
    String toAdd=input.substring(linestart,previous);
    if (next == BreakIterator.DONE) {
      sb.append(toAdd);
      break;
    }
    if (next - linestart > maxLineWidth) {
      sb.append(toAdd + '\n');
      linestart=previous;
    }
 else {
      int newLineIndex=toAdd.lastIndexOf('\n');
      if (newLineIndex != -1) {
        sb.append(toAdd.substring(0,newLineIndex + 1));
        linestart+=newLineIndex + 1;
      }
    }
    previous=next;
  }
  return sb.toString();
}

public static String lineWrap ( String input , int maxLineWidth ) { StringBuffer sb = new StringBuffer ( ) ; BreakIterator biterator = BreakIterator . getLineInstance ( ) ; biterator . setText ( input ) ; int linestart = 0 ; int previous = 0 ; while ( true ) { int next = biterator . next ( ) ; String toAdd = input . substring ( linestart , previous ) ; if ( next == BreakIterator . DONE ) { sb . append ( toAdd ) ; break ; } if ( next - linestart > maxLineWidth ) { sb . append ( toAdd + '\n' ) ; linestart = previous ; } else { int newLineIndex = toAdd . lastIndexOf ( '\n' ) ; if ( newLineIndex != - 1 ) { sb . append ( toAdd . substring ( 0 , newLineIndex + 1 ) ) ; linestart += newLineIndex + 1 ; } } previous = next ; } return sb . toString ( ) ; }
implements simple line breaking . reformats the given string by introducing line breaks so that , ideally , no line exceeds the given number of characters . line breaks are assumed to be indicated by newline characters . existing line breaks are left in the input text .
