3 import java.util.ArrayList;
5 import java.util.regex.Pattern;
7 public class StringUtils
9 private static final Pattern DELIMITERS_PATTERN = Pattern
10 .compile(".*='[^']*(?!')");
12 private static final boolean DEBUG = false;
15 * Returns a new character array, after inserting characters into the given
19 * the character array to insert into
21 * the 0-based position for insertion
23 * the number of characters to insert
25 * the character to insert
27 public static final char[] insertCharAt(char[] in, int position,
30 char[] tmp = new char[in.length + count];
32 if (position >= in.length)
34 System.arraycopy(in, 0, tmp, 0, in.length);
39 System.arraycopy(in, 0, tmp, 0, position);
49 if (position < in.length)
51 System.arraycopy(in, position, tmp, index, in.length - position);
65 public static final char[] deleteChars(char[] in, int from, int to)
67 if (from >= in.length || from < 0)
77 System.arraycopy(in, 0, tmp, 0, from);
82 tmp = new char[in.length - to + from];
83 System.arraycopy(in, 0, tmp, 0, from);
84 System.arraycopy(in, to, tmp, from, in.length - to);
90 * Returns the last part of 'input' after the last occurrence of 'token'. For
91 * example to extract only the filename from a full path or URL.
95 * a delimiter which must be in regular expression format
98 public static String getLastToken(String input, String token)
108 String[] st = input.split(token);
109 return st[st.length - 1];
113 * Parses the input string into components separated by the delimiter. Unlike
114 * String.split(), this method will ignore occurrences of the delimiter which
115 * are nested within single quotes in name-value pair values, e.g. a='b,c'.
119 * @return elements separated by separator
121 public static String[] separatorListToArray(String input, String delimiter)
123 int seplen = delimiter.length();
124 if (input == null || input.equals("") || input.equals(delimiter))
128 List<String> jv = new ArrayList<String>();
129 int cp = 0, pos, escape;
130 boolean wasescaped = false, wasquoted = false;
131 String lstitem = null;
132 while ((pos = input.indexOf(delimiter, cp)) >= cp)
134 escape = (pos > 0 && input.charAt(pos - 1) == '\\') ? -1 : 0;
135 if (wasescaped || wasquoted)
137 // append to previous pos
138 jv.set(jv.size() - 1,
139 lstitem = lstitem + delimiter
140 + input.substring(cp, pos + escape));
144 jv.add(lstitem = input.substring(cp, pos + escape));
147 wasescaped = escape == -1;
148 // last separator may be in an unmatched quote
149 wasquoted = DELIMITERS_PATTERN.matcher(lstitem).matches();
151 if (cp < input.length())
153 String c = input.substring(cp);
154 if (wasescaped || wasquoted)
156 // append final separator
157 jv.set(jv.size() - 1, lstitem + delimiter + c);
161 if (!c.equals(delimiter))
169 String[] v = jv.toArray(new String[jv.size()]);
173 System.err.println("Array from '" + delimiter
174 + "' separated List:\n" + v.length);
175 for (int i = 0; i < v.length; i++)
177 System.err.println("item " + i + " '" + v[i] + "'");
184 System.err.println("Empty Array from '" + delimiter
185 + "' separated List");
191 * Returns a string which contains the list elements delimited by the
192 * separator. Null items are ignored. If the input is null or has length zero,
193 * a single delimiter is returned.
197 * @return concatenated string
199 public static String arrayToSeparatorList(String[] list, String separator)
201 StringBuffer v = new StringBuffer();
202 if (list != null && list.length > 0)
204 for (int i = 0, iSize = list.length; i < iSize; i++)
212 // TODO - escape any separator values in list[i]
218 System.err.println("Returning '" + separator
219 + "' separated List:\n");
220 System.err.println(v);
226 System.err.println("Returning empty '" + separator
227 + "' separated List\n");
229 return "" + separator;