Here you can find the source of getCollection(final String iText, final int iStartPosition, final Collection
public static int getCollection(final String iText, final int iStartPosition, final Collection<String> iCollection)
//package com.java2s; /*// ww w . jav a 2 s . c o m * Copyright 1999-2010 Luca Garulli (l.garulli--at--orientechnologies.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.Collection; public class Main { public static final char COLLECTION_BEGIN = '['; public static final char COLLECTION_END = ']'; public static final char COLLECTION_SEPARATOR = ','; public static int getCollection(final String iText, final int iStartPosition, final Collection<String> iCollection) { final StringBuilder buffer = new StringBuilder(); int openPos = iText.indexOf(COLLECTION_BEGIN, iStartPosition); if (openPos == -1) return -1; int currentPos, deep; for (currentPos = openPos + 1, deep = 1; deep > 0; currentPos++) { if (currentPos >= iText.length()) { return -1; } char c = iText.charAt(currentPos); if (buffer.length() == 0 && c == ' ') { continue; } switch (c) { case COLLECTION_BEGIN: buffer.append(c); deep++; break; case COLLECTION_END: if (deep > 1) buffer.append(c); deep--; break; case COLLECTION_SEPARATOR: if (deep > 1) { buffer.append(c); } else { iCollection.add(buffer.toString().trim()); buffer.setLength(0); } break; default: buffer.append(c); } } iCollection.add(buffer.toString().trim()); return --currentPos; } public static int indexOf(final String iSource, final int iBegin, char... iChars) { if (iChars.length == 1) // ONE CHAR: USE JAVA INDEXOF return iSource.indexOf(iChars[0], iBegin); final int len = iSource.length(); for (int i = iBegin; i < len; ++i) { for (int k = 0; k < iChars.length; ++k) { final char c = iSource.charAt(i); if (c == iChars[k]) return i; } } return -1; } }