Java String Split by Delimiter fastSplit(String string, String delimiter)

Here you can find the source of fastSplit(String string, String delimiter)

Description

fast Split

License

Apache License

Declaration

public static String[] fastSplit(String string, String delimiter) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF 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
 *  //from w  w w .  jav  a  2  s.  c om
 *   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 String[] fastSplit(String string, String delimiter) {
        return fastSplit(string, delimiter, true);
    }

    public static String[] fastSplit(String string, String delimiter, boolean strict) {
        return fastSplit(string, delimiter, strict, false);
    }

    public static String[] fastSplit(String string, String delimiter, boolean strict, boolean template) {
        if (string == null) {
            return new String[0];
        }

        if (string.equals("")) { //$NON-NLS-1$
            return new String[] { "" }; //$NON-NLS-1$
        }

        List<String> tmpResults = new ArrayList<String>();
        int delimiterLength = delimiter.length();
        int delimiterIndex = 0;
        int fromIndex = 0;
        int stringLen = string.length();
        int index = 0;

        // collect all the tokens
        while ((index != -1)) {

            // if the delimiter is at the end of the string
            if (fromIndex >= string.length()) {
                if (strict) {
                    tmpResults.add(""); //$NON-NLS-1$
                }
                break;
            }

            if (template) {
                boolean done = false;
                int brackets = 0;
                index = fromIndex;
                int maxIndex = string.length();
                while (!done) {
                    if (index >= maxIndex) {
                        index = -1;
                        done = true;
                    } else if (string.startsWith(delimiter, index) && brackets == 0) {
                        done = true;
                    } else {
                        if (string.charAt(index) == '{') {
                            ++brackets;
                        } else if (string.charAt(index) == '}') {
                            brackets = (brackets == 0 ? 0 : brackets - 1);
                        }
                        ++index;
                    }
                }
            } else {
                index = string.indexOf(delimiter, fromIndex);
            }

            if (index == -1) {
                delimiterIndex = stringLen;
            } else {
                delimiterIndex = index;
            }

            tmpResults.add(string.substring(fromIndex, delimiterIndex));
            fromIndex = delimiterIndex + delimiterLength;
        }

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

Related

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