Here you can find the source of explode(String input, final char delimiter, final char escape, final int capacity)
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 |
public static String[] explode(String input, final char delimiter, final char escape, final int capacity)
//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()]); } }