Java String Wrap wrapText(String line, int maxLineLength)

Here you can find the source of wrapText(String line, int maxLineLength)

Description

wrap Text

License

LGPL

Declaration

private static List<String> wrapText(String line, int maxLineLength) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.util.ArrayList;
import java.util.List;

public class Main {
    private static List<String> wrapText(String line, int maxLineLength) {
        // TODO: This does not handle lines containing \n characters
        List<String> wrappedLines = new ArrayList<>();
        while (line.length() >= maxLineLength) {
            int lastSpaceIndex = line.substring(0, maxLineLength).lastIndexOf(" ");
            if (lastSpaceIndex == -1) {
                break;
            }/*from w  w w  . j a v  a 2 s.  c o m*/
            wrappedLines.add(line.substring(0, lastSpaceIndex));
            line = line.substring(lastSpaceIndex + 1, line.length());
        }
        wrappedLines.add(line);
        return wrappedLines;
    }
}

Related

  1. wordWrap(String input, int width)
  2. wrapString(String str)
  3. wrapString(String text, int maxLength)
  4. wrapText(String description)
  5. wrapText(String inString, String newline, int wrapColumn)
  6. wrapText(String text, int len)
  7. wrapText(String text, int len, boolean wordWrap)
  8. wrapText(String txt, char breakChar, String lineDelim, int numChars)