Java String Split by Delimiter splitWhileKeepingParentheses(String value, char delimiter)

Here you can find the source of splitWhileKeepingParentheses(String value, char delimiter)

Description

Splits the given String around the matches defined by the given delimiter into an array while not breaking up areas that are marked with parentheses ().

License

Open Source License

Parameter

Parameter Description
value the String which should be split into an array
delimiter the delimiter which marks the boundaries of the array

Return

an array, when the delimiter was not found, the array will only have a single element.

Declaration

public static String[] splitWhileKeepingParentheses(String value, char delimiter) 

Method Source Code


//package com.java2s;
/*/*from   w  ww  .j  a v a2 s  .c o  m*/
 * Created on 24-Nov-2003 at 14:38:43
 *
 * Copyright (c) 2004-2005 Robert Virkus / Enough Software
 *
 * This file is part of J2ME Polish.
 *
 * J2ME Polish is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * J2ME Polish is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with J2ME Polish; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Commercial licenses are also available, please
 * refer to the accompanying LICENSE.txt or visit
 * http://www.j2mepolish.org for details.
 */

import java.util.ArrayList;

public class Main {
    /**
     * Splits the given String around the matches defined by the given delimiter into an array while not breaking up areas that are marked with parentheses ().
     * Example:
     * <pre>TextUtil.split("one; two; three(test;test2)", ';')</pre> results into the array
     * <pre>{"one", " two", " three(test;test2)"}</pre>.
     *
     * @param value the String which should be split into an array
     * @param delimiter the delimiter which marks the boundaries of the array 
     * @return an array, when the delimiter was not found, the array will only have a single element.
     */
    public static String[] splitWhileKeepingParentheses(String value, char delimiter) {
        char[] valueChars = value.toCharArray();
        int lastIndex = 0;
        ArrayList strings = null;
        boolean isParenthesesOpened = false;
        for (int i = 0; i < valueChars.length; i++) {
            char c = valueChars[i];
            if (c == delimiter && !isParenthesesOpened) {
                if (strings == null) {
                    strings = new ArrayList();
                }
                strings.add(new String(valueChars, lastIndex, i - lastIndex));
                lastIndex = i + 1;
            } else if (c == ')') {
                isParenthesesOpened = false;
            } else if (c == '(') {
                isParenthesesOpened = true;
            }
        }
        if (strings == null) {
            return new String[] { value };
        }
        // add tail:
        strings.add(new String(valueChars, lastIndex, valueChars.length - lastIndex));
        return (String[]) strings.toArray(new String[strings.size()]);
    }
}

Related

  1. splitToList(final String string, final String delim, final int limit)
  2. splitToList(String a_text, String a_delimiter)
  3. splitToList(String s, String delimRegEx)
  4. splitToList(String str, char delimiter)
  5. splitUpTo(String s, String delimiter, int cnt)
  6. splitWithEscapeChar(String s, char delimiter, char escape)
  7. stringSplitter(String inStr, String delimiterStr)
  8. tokenizerSplit(String string, String delimiter)
  9. toTextArray(String text, String delimiter)