Here you can find the source of splitLines(String paragraph, int lineLength)
public static List<String> splitLines(String paragraph, int lineLength)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.List; public class Main { public static List<String> splitLines(String paragraph, int lineLength) { List<String> lines = new ArrayList<String>(); int lineStart = 0; int lastSpace = -1; while (true) { int nextSpace = paragraph.indexOf(' ', lastSpace + 1); if (nextSpace == -1) { // End of paragraph lines.add(paragraph.substring(lineStart, paragraph.length())); break; } else if (nextSpace - lineStart > lineLength) { if (lastSpace + 1 == lineStart) { // Block of more than lineLength without a space in it - has to be one line lastSpace = nextSpace; }//from w w w .ja v a 2s . com // End of line at last space lines.add(paragraph.substring(lineStart, lastSpace)); lineStart = lastSpace + 1; } else { lastSpace = nextSpace; } } return lines; } }