Java String Split by Delimiter splitNestedString(String params, String delimStr, int numLeft, int numRight)

Here you can find the source of splitNestedString(String params, String delimStr, int numLeft, int numRight)

Description

Splits a string, returning the string separated by the delimiter, with only the numLeft and numRight taken from the appropriate sides

License

Open Source License

Declaration

static public List<String> splitNestedString(String params, String delimStr, int numLeft, int numRight) 

Method Source Code

//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;
    }
}

Related

  1. splitFast(String text, char delim)
  2. splitForChar(final String string, final char delimiter)
  3. splitHtmlTagKeepDelimiter(String tag, String input)
  4. splitKeepDelimiter(String delimiter, String input)
  5. splitList(String data, String delims)
  6. splitNoCoalesce(String s, char delimiter)
  7. splitOnEntireString(String target, String delimiter)
  8. splitSimple(String delimiter, String str)
  9. splitSimpleLimit(String str, String delimiter, int limitSize)