Java String Split by Separator split(String text, String separators, String brackets)

Here you can find the source of split(String text, String separators, String brackets)

Description

split

License

Open Source License

Declaration

public static List<String> split(String text, String separators, String brackets) 

Method Source Code

//package com.java2s;
/*/*  ww  w  .  j a  va  2s  . co  m*/
 * Copyright (c) 2007-2013, 2015 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * @since 3.4
     */
    public static List<String> split(String text, String separators, String brackets) {
        List<String> result = new ArrayList<String>();

        StringBuilder builder = new StringBuilder();
        int length = text.length();
        int bracketLevel = 0;

        for (int i = 0; i < length; i++) {
            char c = text.charAt(i);

            if (bracketLevel == 0 && separators.indexOf(c) != -1) {
                result.add(builder.toString());
                builder.setLength(0);
            } else {
                builder.append(c);
            }

            if (brackets != null) {
                int bracket = brackets.indexOf(c);
                if (bracket != -1) {
                    if ((bracket & 1) == 0) {
                        // Opening bracket
                        ++bracketLevel;
                    } else {
                        // Closing bracket
                        --bracketLevel;
                    }
                }
            }
        }

        result.add(builder.toString());
        return result;
    }
}

Related

  1. split(String str, String separator)
  2. split(String str, String separator)
  3. split(String str, String separator, boolean preserveEmptyToken)
  4. split(String string, char separator)
  5. split(String strToSplit, String strSeparator, int iLimit)
  6. splitAll(String str, char separatorChar)
  7. splitAndTrim(String tags, String separator)
  8. splitBySeparator(String path, String separator)
  9. splitByStringSeparator(String theString, String separatorString)