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

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

Description

fast Split

License

Apache License

Declaration

public static List<String> fastSplit(final String text, char separator) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w  .j av a2s  .  c om*/
* Copyright 2015 Bizosys Technologies Limited
*
* Licensed to the Bizosys Technologies Limited (Bizosys) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The Bizosys licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static List<String> fastSplit(final String text, char separator) {
        return fastSplit(null, text, separator);
    }

    public static List<String> fastSplit(List<String> result, final String text, char separator) {
        if (isEmpty(text))
            return null;

        if (null == result)
            result = new ArrayList<String>();
        int index1 = 0;
        int index2 = text.indexOf(separator);

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

            if (index1 < text.length() - 1) {
                result.add(text.substring(index1 + 1));
            }

        } else {
            result.add(text);
        }

        return result;
    }

    public static final void fastSplit(final String[] result, final int[] positions, final String text,
            final char separator) {

        if (text == null)
            return;
        if (text.length() == 0)
            return;

        int index1 = 0;
        int index2 = text.indexOf(separator);

        int pos = -1;
        int resultSeq = 0;
        if (index2 >= 0) {
            String token = null;
            while (index2 >= 0) {
                pos++;
                for (int aPos : positions) {
                    if (pos != aPos)
                        continue;
                    token = text.substring(index1, index2);
                    result[resultSeq++] = token;
                    break;
                }
                index1 = index2 + 1;
                index2 = text.indexOf(separator, index1);
                if (index2 < 0)
                    index1--;
            }

            if (index1 < text.length() - 1) {
                pos++;
                for (int aPos : positions) {
                    if (pos != aPos)
                        continue;
                    result[resultSeq++] = text.substring(index1 + 1);
                    break;
                }
            }

        } else {
            pos++;
            for (int aPos : positions) {
                if (pos != aPos)
                    continue;
                result[resultSeq++] = text;
                break;
            }
        }
    }

    /**
     * Checks if a string is empty
     * @param text String value to check
     * @return true/false.
     */
    public static boolean isEmpty(String text) {
        if (null == text)
            return true;
        if (text.length() == 0)
            return true;
        else
            return false;
    }
}

Related

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