Java String Split by Length splitIntoLine(String input, int maxCharInLine)

Here you can find the source of splitIntoLine(String input, int maxCharInLine)

Description

split Into Line

License

Apache License

Declaration

public static String splitIntoLine(String input, int maxCharInLine) 

Method Source Code

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

import java.util.*;

public class Main {
    public static String splitIntoLine(String input, int maxCharInLine) {

        StringTokenizer tok = new StringTokenizer(input, " ");
        StringBuilder output = new StringBuilder(input.length());
        int lineLen = 0;
        while (tok.hasMoreTokens()) {
            String word = tok.nextToken();

            while (word.length() > maxCharInLine) {
                output.append(word.substring(0, maxCharInLine - lineLen)
                        + "\n");
                word = word.substring(maxCharInLine - lineLen);
                lineLen = 0;/*from w w w  .  ja v  a 2 s.  c  om*/
            }

            if (lineLen + word.length() > maxCharInLine) {
                output.append("\n");
                lineLen = 0;
            }
            output.append(word + " ");

            lineLen += word.length() + 1;
        }
        // output.split();
        // return output.toString();
        return output.toString() + "\n";
    }
}

Related

  1. splitEqually(String _text, int _len)
  2. splitEscapedString(String str, char sep, char esc, int maxPart)
  3. splitFilenames(String text)
  4. splitHtmlString(String stringToSplit, int maxLength)
  5. splitIntoFrames(String src, int maxFrameSize)
  6. splitLines(String paragraph, int lineLength)
  7. splitParams(String string, int max)
  8. splitPreserveAllTokens(String str, String separatorChars, int max)
  9. splitString(String orig, String delim, int max)