3 import java.util.ArrayList;
5 import java.util.regex.Pattern;
8 public class StringUtils
10 private static final Pattern DELIMITERS_PATTERN = Pattern.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,
31 char[] tmp = new char[in.length + count];
33 if (position >= in.length)
35 System.arraycopy(in, 0, tmp, 0, in.length);
40 System.arraycopy(in, 0, tmp, 0, position);
50 if (position < in.length)
52 System.arraycopy(in, position, tmp, index,
53 in.length - position);
67 public static final char[] deleteChars(char[] in, int from, int to)
69 if (from >= in.length || from < 0)
79 System.arraycopy(in, 0, tmp, 0, from);
84 tmp = new char[in.length - to + from];
85 System.arraycopy(in, 0, tmp, 0, from);
86 System.arraycopy(in, to, tmp, from, in.length - to);
92 * Returns the last part of 'input' after the last occurrence of 'token'. For
93 * example to extract only the filename from a full path or URL.
97 * a delimiter which must be in regular expression format
100 public static String getLastToken(String input, String token)
110 String[] st = input.split(token);
111 return st[st.length - 1];
115 * Parses the input string into components separated by the delimiter. Unlike
116 * String.split(), this method will ignore occurrences of the delimiter which
117 * are nested within single quotes in name-value pair values, e.g. a='b,c'.
121 * @return elements separated by separator
123 public static String[] separatorListToArray(String input, String delimiter)
125 int seplen = delimiter.length();
126 if (input == null || input.equals("") || input.equals(delimiter))
130 List<String> jv = new ArrayList<String>();
131 int cp = 0, pos, escape;
132 boolean wasescaped = false, wasquoted = false;
133 String lstitem = null;
134 while ((pos = input.indexOf(delimiter, cp)) >= cp)
136 escape = (pos > 0 && input.charAt(pos - 1) == '\\') ? -1 : 0;
137 if (wasescaped || wasquoted)
139 // append to previous pos
140 jv.set(jv.size() - 1,
141 lstitem = lstitem + delimiter
142 + input.substring(cp, pos + escape));
146 jv.add(lstitem = input.substring(cp, pos + escape));
149 wasescaped = escape == -1;
150 // last separator may be in an unmatched quote
151 wasquoted = DELIMITERS_PATTERN.matcher(lstitem).matches();
153 if (cp < input.length())
155 String c = input.substring(cp);
156 if (wasescaped || wasquoted)
158 // append final separator
159 jv.set(jv.size() - 1, lstitem + delimiter + c);
163 if (!c.equals(delimiter))
171 String[] v = jv.toArray(new String[jv.size()]);
175 System.err.println("Array from '" + delimiter
176 + "' separated List:\n" + v.length);
177 for (int i = 0; i < v.length; i++)
179 System.err.println("item " + i + " '" + v[i] + "'");
186 System.err.println("Empty Array from '" + delimiter
187 + "' separated List");
193 * Returns a string which contains the list elements delimited by the
194 * separator. Null items are ignored. If the input is null or has length zero,
195 * a single delimiter is returned.
199 * @return concatenated string
201 public static String arrayToSeparatorList(String[] list, String separator)
203 StringBuffer v = new StringBuffer();
204 if (list != null && list.length > 0)
206 for (int i = 0, iSize = list.length; i < iSize; i++)
214 // TODO - escape any separator values in list[i]
220 System.err.println("Returning '" + separator
221 + "' separated List:\n");
222 System.err.println(v);
228 System.err.println("Returning empty '" + separator
229 + "' separated List\n");
231 return "" + separator;