Java String Split by Separator fastSplit(String text, char separator)

Here you can find the source of fastSplit(String text, char separator)

Description

A faster and simpler character-based string splitter based on http://forums.sun.com/thread.jspa?threadID=766801&start=0&tstart=0

License

Open Source License

Parameter

Parameter Description
text parsed input string
separator separator character

Return

splitted string array

Declaration

public static String[] fastSplit(String text, char separator) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

public class Main {
    /**//www . j a v a 2 s  . c om
     * A faster and simpler character-based string splitter
     * based on http://forums.sun.com/thread.jspa?threadID=766801&start=0&tstart=0
     *
     * @param text parsed input string
     * @param separator separator character
     * @return splitted string array
     */
    public static String[] fastSplit(String text, char separator) {
        ArrayList<String> result = new ArrayList<String>();
        if (text != null && text.length() > 0) {
            int index1 = 0;
            int index2 = text.indexOf(separator);

            while (index2 >= 0) {
                String token = text.substring(index1, index2);
                result.add(token);
                index1 = index2 + 1;
                index2 = text.indexOf(separator, index1);
            }

            result.add(text.substring(index1));
        }

        return result.toArray(new String[result.size()]);
    }
}

Related

  1. fastSplit(final String text, char separator)
  2. split(byte[] b, int separator)
  3. split(CharSequence seq, char separator)
  4. split(final String expression, final char separator)
  5. split(final String input, final String separator)