Here you can find the source of splitBySeparatorAndParen(String line, char sep)
Parameter | Description |
---|---|
line | The String to be seperated. |
sep | The seperator character, eg a comma |
public static String[] splitBySeparatorAndParen(String line, char sep)
//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); } }