Java String Explode explode(String input, final char delimiter, final char escape, final int capacity)

Here you can find the source of explode(String input, final char delimiter, final char escape, final int capacity)

Description

Separate the components of a String that has been imploded using the specified delimiter and escape character

License

Open Source License

Parameter

Parameter Description
input The imploded String
delimiter The character used to separate the component Strings
escape The character used to escape the delimiter
capacity The anticipated size of the resulting array

Declaration

public static String[] explode(String input, final char delimiter, final char escape, final int capacity) 

Method Source Code

//package com.java2s;
// This package is part of the Spiralcraft project and is licensed under

import java.util.ArrayList;

public class Main {
    /**//from ww w  .  j  a va  2s.c  om
     * Separate the components of a String that has been imploded using the
     *   specified delimiter and escape character
     *   
     * @param input The imploded String
     * @param delimiter The character used to separate the component Strings 
     * @param escape The character used to escape the delimiter
     * @param capacity The anticipated size of the resulting array
     * @return
     */
    public static String[] explode(String input, final char delimiter, final char escape, final int capacity) {
        if (input == null) {
            return null;
        }

        StringBuilder seg = new StringBuilder();
        ArrayList<String> result = new ArrayList<String>(capacity >= 0 ? capacity : 3);

        boolean inEscape = false;
        for (char chr : input.toCharArray()) {
            if (inEscape) {
                inEscape = false;
                seg.append(chr);
            } else if (chr == escape) {
                inEscape = true;
            } else if (chr == delimiter) {
                result.add(seg.toString());
                seg.setLength(0);
            } else {
                seg.append(chr);
            }
        }

        if (inEscape) {
            throw new IllegalArgumentException("Incomplete escape sequence at end of string: " + input);
        }
        result.add(seg.toString());
        return result.toArray(new String[result.size()]);
    }
}

Related

  1. explode(int clr)
  2. explode(String csv)
  3. explode(String data, String delim)
  4. explode(String delim, String string)
  5. explode(String handleStr, String pointStr)
  6. explode(String s, String delim)
  7. explode(String source, String deliminator)
  8. explode(String split, String input)
  9. explode(String src, String sep)