public static String wrap(String s,int n){
  StringBuffer b=new StringBuffer();
  boolean wrapPending=false;
  for (int i=0; i < s.length(); i++) {
    if (i % n == 0 && i > 0) {
      wrapPending=true;
    }
    char c=s.charAt(i);
    if (wrapPending && c == ' ') {
      b.append("\n");
      wrapPending=false;
    }
 else {
      b.append(c);
    }
  }
  return b.toString();
}

public static String wrap ( String s , int n ) { StringBuffer b = new StringBuffer ( ) ; boolean wrapPending = false ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( i % n == 0 && i > 0 ) { wrapPending = true ; } char c = s . charAt ( i ) ; if ( wrapPending && c == ' ' ) { b . append ( "\n" ) ; wrapPending = false ; } else { b . append ( c ) ; } } return b . toString ( ) ; }
line - wraps a string s by inserting cr - lf instead of the first space after the nth columns .
