Java String Split by Line splitBySeparatorAndParen(String line, char sep)

Here you can find the source of splitBySeparatorAndParen(String line, char sep)

Description

Split using a separator, but allow for the separator to occur in nested parentheses without splitting.

License

GNU General Public License

Parameter

Parameter Description
line The String to be seperated.
sep The seperator character, eg a comma

Return

An array of Strings containing the seperated data.

Declaration

public static String[] splitBySeparatorAndParen(String line, char sep) 

Method Source Code

//package com.java2s;
/***************************************
 *            ViPER                    *
 *  The Video Processing               *
 *         Evaluation Resource         *
 *                                     *
 *  Distributed under the GPL license  *
 *        Terms available at gnu.org.  *
 *                                     *
 *  Copyright University of Maryland,  *
 *                      College Park.  *
 ***************************************/

import java.util.*;

public class Main {
    /**/*from   ww w .j a  v  a 2 s .  com*/
     * Split using a separator, but allow for the separator to occur
     * in nested parentheses without splitting.  
     *   E.g.   
     * <pre>
     *     1, (2,3), 4
     * </pre>
     *  would split into 
     * <UL>
     *   <li> 1 </li>
     *   <li> (2,3)  (Doesn't get split, even though it has comma) </li>
     *   <li> 4 </li>
     * </ul>
     *
     * @param line The String to be seperated.
     * @param sep The seperator character, eg a comma
     * @return An array of Strings containing the seperated data.
     * @see #splitBySeparator(String line, char c)
     */
    public static String[] splitBySeparatorAndParen(String line, char sep) {
        boolean withinQuotes = false;
        String newLine = new String(line);
        Vector temp = new Vector();

        int startIndex = 0;
        int nesting = 0;
        for (int i = 0; i < newLine.length(); i++) {
            char c = newLine.charAt(i);
            if (c == '"') {
                withinQuotes = !withinQuotes;
            } else if (!withinQuotes) {
                if (c == '(') {
                    nesting++;
                } else if (c == ')') {
                    nesting--;
                } else {
                    if ((nesting == 0) && (c == sep)) {
                        String s = newLine.substring(startIndex, i);
                        temp.addElement(s);

                        startIndex = i + 1;
                    }
                }
            }
        }
        String s = newLine.substring(startIndex);
        temp.addElement(s);

        String[] result = new String[temp.size()];
        for (int i = 0; i < result.length; i++) {
            result[i] = (String) temp.elementAt(i);
        }
        return (result);
    }
}

Related

  1. Split(String line, String sptr)
  2. splitArguments(String commandLine, boolean backslashesAreLiteral)
  3. splitArgumentsBackslashesAreLiteral(String commandLine)
  4. splitByBrackets(String line)
  5. splitBySeparator(String line, char sep)
  6. splitBySeparatorQuoteAndParen(String line, char sep)
  7. splitCommandLine(String str, boolean extract)
  8. splitCommaSeparated(String line)
  9. splitHiddenNewLine(String s)