Java String Split by Line splitLine(String input, int splitPosition)

Here you can find the source of splitLine(String input, int splitPosition)

Description

split Line

License

Open Source License

Declaration

public static List<String> splitLine(String input, int splitPosition) 

Method Source Code

//package com.java2s;
// SOLUTIONS CORP. OR ITS LICENSORS.

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

public class Main {
    public static List<String> splitLine(String input, int splitPosition) {
        List<String> lines = new ArrayList<String>();

        StringBuilder stringBuilder = new StringBuilder(input.trim());
        boolean linesRemaining = true;
        while (linesRemaining) {
            if (stringBuilder.length() <= splitPosition
                    || stringBuilder.lastIndexOf(" ") < splitPosition) {
                lines.add(stringBuilder.toString());
                linesRemaining = false;// www. j  a  v a 2s .c om
            } else {
                int indexOfSpace = stringBuilder
                        .indexOf(" ", splitPosition);
                String nextLine = stringBuilder.substring(0,
                        indexOfSpace + 1);
                lines.add(nextLine);
                stringBuilder.delete(0, indexOfSpace + 1);
            }
        }

        return lines;
    }

    /**
     * Trim the leading and trailing whitespace from a string.  If a null
     * reference is passed in then return an empty string.
     */
    public static String trim(String string) {
        if (string == null) {
            return "";
        }

        return string.trim();
    }
}

Related

  1. splitInt(String line, String seperator, int def)
  2. splitIntoLines(String str)
  3. splitIntoNonEmptyLines(String s)
  4. splitIntoNonEmptyLinesWithoutNetLogoComments( String s)
  5. splitLine(final String line, final char delimiter, final char qualifier, final int initialSize)
  6. SplitLine(String p_line, String p_delimiter)
  7. splitLines(final String helpMessage)
  8. splitLines(final String string)
  9. splitLines(String raw, int limit)