Java Utililty Methods String Wrap

List of utility methods to do String Wrap

Description

The list of methods to do String Wrap are organized into topic(s).

Method

ListgetWordWrappedText(int maxWidth, String... lines)
This method will calculate word wrappings given a number of lines of text and how wide the text can be printed.
if (maxWidth <= 0) {
    return Arrays.asList(lines);
List<String> result = new ArrayList<String>();
LinkedList<String> linesToBeWrapped = new LinkedList<String>(Arrays.asList(lines));
while (!linesToBeWrapped.isEmpty()) {
    String row = linesToBeWrapped.removeFirst();
    int rowWidth = getColumnWidth(row);
...
StringgetWrappedLine(String line, int lineLength)
Inserts newlines at or before lineLength at spaces or commas.
if (line.length() <= lineLength) {
    return line;
StringBuffer result = new StringBuffer();
char[] lineChars = line.toCharArray();
int lastBreakCharIdx = -1;
ArrayList<Integer> breakPoints = new ArrayList<Integer>();
for (int i = 0; i < lineChars.length; i++) {
...
ListlineWrap(String text, int width, boolean shiftNewLines)
line Wrap
String[] words = text.trim().split(" ");
StringBuilder currentLine = new StringBuilder();
List<String> newLines = new ArrayList<String>();
int currentLength = 0;
for (int i = 0; i < words.length; i++) {
    currentLine.append(words[i] + " ");
    currentLength = currentLine.length();
    int nextWordLength = 0;
...
String[]wordWrap(final String rawString, final int lineLength)
word Wrap
if (rawString == null) {
    return new String[] { "" };
if ((rawString.length() <= lineLength) && (!rawString.contains("\n"))) {
    return new String[] { rawString };
final char[] rawChars = (rawString + ' ').toCharArray();
StringBuilder word = new StringBuilder();
...
ListwordWrap(String entry)
word Wrap
int ind = 0;
List<String> out = new ArrayList<>();
while (ind <= entry.length()) {
    int nind = ind + 40;
    if (nind < entry.length()) {
        if (entry.charAt(nind) != ' ') {
            while (entry.charAt(nind) != ' ' && nind != 0) {
                nind--;
...
ListwordWrap(String input, int width)
word Wrap
ArrayList<String> output = new ArrayList<>();
final char[] chars = input.toCharArray();
final int N = input.length();
if (N < width) {
    return Arrays.asList(input.split("\n"));
int prevLine = 0;
while (true) {
...
StringwrapString(String str)
wrap String
return wrapString(str, MAX_LENGTH);
ListwrapString(String text, int maxLength)
wrap String
List<String> tmpStrings = new ArrayList<String>();
if (text == null) {
    return tmpStrings;
text = text.trim();
if (text.length() <= maxLength) {
    tmpStrings.add(text);
    return tmpStrings;
...
ListwrapText(String description)
wrap Text
description = description.trim();
return Arrays.asList(description.split("\n"));
StringwrapText(String inString, String newline, int wrapColumn)
Lines wraps a block of text.
StringTokenizer lineTokenizer = new StringTokenizer(inString, newline, true);
StringBuffer stringBuffer = new StringBuffer();
while (lineTokenizer.hasMoreTokens()) {
    try {
        String nextLine = lineTokenizer.nextToken();
        if (nextLine.length() > wrapColumn) {
            nextLine = wrapLine(nextLine, newline, wrapColumn);
        stringBuffer.append(nextLine);
    } catch (NoSuchElementException e) {
        break;
return (stringBuffer.toString());