Java String Split by Length splitLines(String paragraph, int lineLength)

Here you can find the source of splitLines(String paragraph, int lineLength)

Description

split Lines

License

Open Source License

Declaration

public static List<String> splitLines(String paragraph, int lineLength) 

Method Source Code


//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;
    }
}

Related

  1. splitEscapedString(String str, char sep, char esc, int maxPart)
  2. splitFilenames(String text)
  3. splitHtmlString(String stringToSplit, int maxLength)
  4. splitIntoFrames(String src, int maxFrameSize)
  5. splitIntoLine(String input, int maxCharInLine)
  6. splitParams(String string, int max)
  7. splitPreserveAllTokens(String str, String separatorChars, int max)
  8. splitString(String orig, String delim, int max)
  9. splitString(String s, int maxSize)