Here you can find the source of splitNestedString(String params, String delimStr, int numLeft, int numRight)
static public List<String> splitNestedString(String params, String delimStr, int numLeft, int numRight)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.List; public class Main { /**//from ww w .j ava2 s. co m * Splits a string, returning the string separated by the delimiter, with only the numLeft and numRight taken from the appropriate sides */ static public List<String> splitNestedString(String params, String delimStr, int numLeft, int numRight) { ArrayList<String> args = new ArrayList<String>(numLeft); ArrayList<String> endArgs = new ArrayList<String>(numRight); //We need to rip chunks out of the left side int startIndex, endIndex; startIndex = 0; endIndex = 0; for (int i = 0; i < numLeft; i++) { endIndex = params.indexOf(delimStr, startIndex); if (endIndex == -1) throw new IllegalArgumentException( "Could only find " + i + " left args before not finding the delimeter"); args.add(params.substring(startIndex, endIndex)); startIndex = endIndex + delimStr.length(); } int middleStart = startIndex; //Now go find stuff on the right endIndex = params.length(); startIndex = endIndex; for (int i = 0; i < numRight; i++) { startIndex = params.lastIndexOf(delimStr, endIndex - 1); if (startIndex == -1) throw new IllegalArgumentException( "Could only find " + i + " right args before not finding the delimeter"); endArgs.add(params.substring(startIndex + delimStr.length(), endIndex)); endIndex = startIndex; } int middleEnd = startIndex; if (middleStart >= middleEnd) { throw new IllegalArgumentException( "The number of left and right args doesn't leave anything in the middle"); } args.add(params.substring(middleStart, middleEnd)); for (int i = endArgs.size() - 1; i >= 0; i--) { args.add(endArgs.get(i)); } return args; } }