Java String Split by Delimiter split(final String input, final char delimiter)

Here you can find the source of split(final String input, final char delimiter)

Description

split

License

Open Source License

Declaration

public static List<String> split(final String input, final char delimiter) 

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> split(final String input, final char delimiter) {
        return split(input, delimiter, false);
    }//from  w w w. j a v  a2  s.c  o m

    public static List<String> split(final String input, final char delimiter, final boolean emptyTokens) {
        final ArrayList<String> list = new ArrayList<String>();
        if (input == null || input.length() < 1)
            return list;

        int currentChar = 0;
        int nextChar = input.indexOf(delimiter);
        while (nextChar > 0) {
            if (input.charAt(currentChar) != delimiter)
                list.add(input.substring(currentChar, nextChar));
            else if (emptyTokens)
                list.add("");
            currentChar = nextChar;
            currentChar++;
            nextChar = input.indexOf(delimiter, currentChar);
        }
        if (emptyTokens && currentChar == input.length())
            list.add("");
        else
            list.add(input.substring(currentChar));
        return list;
    }

    public static List<String> split(final String input, final char delimiter, final int maxTokens) {
        return split(input, delimiter, maxTokens, false);
    }

    public static List<String> split(final String input, final char delimiter, final int maxTokens,
            final boolean emptyTokens) {
        if (maxTokens <= 0)
            return split(input, delimiter, emptyTokens);

        final ArrayList<String> list = new ArrayList<String>();
        if (input == null || input.length() < 1)
            return list;

        int currentChar = 0;
        int nextChar = input.indexOf(delimiter);
        while (nextChar > 0) {
            if (input.charAt(currentChar) != delimiter) {
                if (list.size() == maxTokens - 1) {
                    list.add(input.substring(currentChar));
                    return list;
                }
                list.add(input.substring(currentChar, nextChar));
            } else if (emptyTokens) {
                if (list.size() == maxTokens - 1) {
                    list.add(input.substring(currentChar));
                    return list;
                }
                list.add("");
            }
            currentChar = nextChar;
            currentChar++;
            nextChar = input.indexOf(delimiter, currentChar);
        }
        if (emptyTokens && currentChar == input.length())
            list.add("");
        else
            list.add(input.substring(currentChar));
        return list;
    }
}

Related

  1. fastSplit(String string, char delimiter)
  2. fastSplit(String string, String delimiter)
  3. split(final boolean enable, final String value, final char delimiter)
  4. split(final String input, final String delimiter, final boolean removeEmpty)
  5. split(final String src, final char delim)
  6. split(final String str, final char delim)
  7. split(final String str, final String delimiter)