public String getFormatedSequence ( final int width ) { if ( sequence == null ) { return "" ; } assert width >= 0 : "Wrong width parameter " ; final StringBuilder sb = new StringBuilder ( sequence ) ; int nchunks = sequence . length ( ) / width ; // add up inserted new line chars nchunks = ( nchunks + sequence . length ( ) ) / width ; int nlineCharcounter = 0 ; for ( int i = 1 ; i <= nchunks ; i ++ ) { final int insPos = width * i + nlineCharcounter ; // to prevent inserting new line in the very end of a sequence then // it would have failed. // Also covers the case when the sequences shorter than width if ( sb . length ( ) <= insPos ) { break ; } sb . insert ( insPos , "\n" ) ; nlineCharcounter ++ ; } return sb . toString ( ) ; }
Format sequence per width letter in one string . Without spaces .