Here you can find the source of splitByBrackets(String line)
Parameter | Description |
---|---|
line | The data to be split. |
Parameter | Description |
---|---|
IllegalArgumentException | if brackets are unbalanced |
public static String[] splitByBrackets(String line)
//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 { /**/*w ww. ja v a 2s.c om*/ * Extracts a list of all strings contained within brackets. * Assumes list contains bracketed elements, as in: * <pre> * [ foo ] [1.2 1.2] [hi] * </pre> * This will return {" foo ", "1.2 1.2", "hi"}. * * @param line The data to be split. * @throws IllegalArgumentException if brackets are unbalanced * @return An Array containing the Strings inside the brackets. */ public static String[] splitByBrackets(String line) { if (line == null) { return (new String[0]); } int start = 0; int state = 0; List L = new LinkedList(); for (int i = 0; i < line.length(); i++) { char c = line.charAt(i); if (c == '[') { if (0 == state++) { start = i + 1; } } else if (c == ']') { if (0 == --state) { L.add(line.substring(start, i)); } if (state < 0) { throw new IllegalArgumentException("Found unexpected end bracket in string: " + line); } } } if (state > 0) { throw new IllegalArgumentException("String requires balanced brackets: " + line); } return (String[]) L.toArray(new String[L.size()]); } }