public String [ ] splitString ( String text , int maxChars , String [ ] splitValues ) { String restOfText = text ; List < String > list = new ArrayList < String > ( ) ; while ( restOfText . length ( ) > maxChars ) { String thisLine = restOfText . substring ( 0 , maxChars ) ; int lineEnd = 0 ; for ( int i = 0 ; i < splitValues . length ; i ++ ) { int lastIndex = thisLine . lastIndexOf ( splitValues [ i ] ) ; if ( lastIndex > lineEnd ) { lineEnd = lastIndex ; } } if ( lineEnd == 0 ) { list . add ( thisLine ) ; restOfText = restOfText . substring ( maxChars ) ; } else { list . add ( thisLine . substring ( 0 , lineEnd ) ) ; restOfText = restOfText . substring ( lineEnd ) ; } } list . add ( restOfText ) ; return list . toArray ( new String [ list . size ( ) ] ) ; }
Split the given string on the given split values ensuring the maximum number of characters in each token .