2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.regex.Pattern;
27 public class StringUtils
29 private static final Pattern DELIMITERS_PATTERN = Pattern
30 .compile(".*='[^']*(?!')");
32 private static final boolean DEBUG = false;
35 * Returns a new character array, after inserting characters into the given
39 * the character array to insert into
41 * the 0-based position for insertion
43 * the number of characters to insert
45 * the character to insert
47 public static final char[] insertCharAt(char[] in, int position,
50 char[] tmp = new char[in.length + count];
52 if (position >= in.length)
54 System.arraycopy(in, 0, tmp, 0, in.length);
59 System.arraycopy(in, 0, tmp, 0, position);
69 if (position < in.length)
71 System.arraycopy(in, position, tmp, index, in.length - position);
85 public static final char[] deleteChars(char[] in, int from, int to)
87 if (from >= in.length || from < 0)
97 System.arraycopy(in, 0, tmp, 0, from);
102 tmp = new char[in.length - to + from];
103 System.arraycopy(in, 0, tmp, 0, from);
104 System.arraycopy(in, to, tmp, from, in.length - to);
110 * Parses the input string into components separated by the delimiter. Unlike
111 * String.split(), this method will ignore occurrences of the delimiter which
112 * are nested within single quotes in name-value pair values, e.g. a='b,c'.
116 * @return elements separated by separator
118 public static String[] separatorListToArray(String input,
121 int seplen = delimiter.length();
122 if (input == null || input.equals("") || input.equals(delimiter))
126 List<String> jv = new ArrayList<>();
127 int cp = 0, pos, escape;
128 boolean wasescaped = false, wasquoted = false;
129 String lstitem = null;
130 while ((pos = input.indexOf(delimiter, cp)) >= cp)
132 escape = (pos > 0 && input.charAt(pos - 1) == '\\') ? -1 : 0;
133 if (wasescaped || wasquoted)
135 // append to previous pos
136 jv.set(jv.size() - 1, lstitem = lstitem + delimiter
137 + input.substring(cp, pos + escape));
141 jv.add(lstitem = input.substring(cp, pos + escape));
144 wasescaped = escape == -1;
145 // last separator may be in an unmatched quote
146 wasquoted = DELIMITERS_PATTERN.matcher(lstitem).matches();
148 if (cp < input.length())
150 String c = input.substring(cp);
151 if (wasescaped || wasquoted)
153 // append final separator
154 jv.set(jv.size() - 1, lstitem + delimiter + c);
158 if (!c.equals(delimiter))
166 String[] v = jv.toArray(new String[jv.size()]);
170 System.err.println("Array from '" + delimiter
171 + "' separated List:\n" + v.length);
172 for (int i = 0; i < v.length; i++)
174 System.err.println("item " + i + " '" + v[i] + "'");
182 "Empty Array from '" + delimiter + "' separated List");
188 * Returns a string which contains the list elements delimited by the
189 * separator. Null items are ignored. If the input is null or has length zero,
190 * a single delimiter is returned.
194 * @return concatenated string
196 public static String arrayToSeparatorList(String[] list, String separator)
198 StringBuffer v = new StringBuffer();
199 if (list != null && list.length > 0)
201 for (int i = 0, iSize = list.length; i < iSize; i++)
209 // TODO - escape any separator values in list[i]
216 .println("Returning '" + separator + "' separated List:\n");
217 System.err.println(v);
224 "Returning empty '" + separator + "' separated List\n");
226 return "" + separator;
230 * Converts a list to a string with a delimiter before each term except the
231 * first. Returns an empty string given a null or zero-length argument. This
232 * can be replaced with StringJoiner in Java 8.
238 public static String listToDelimitedString(List<String> terms,
241 StringBuilder sb = new StringBuilder(32);
242 if (terms != null && !terms.isEmpty())
244 boolean appended = false;
245 for (String term : terms)
255 return sb.toString();
259 * Convenience method to parse a string to an integer, returning 0 if the
260 * input is null or not a valid integer
265 public static int parseInt(String s)
268 if (s != null && s.length() > 0)
272 result = Integer.parseInt(s);
273 } catch (NumberFormatException ex)
281 * Compares two versions formatted as e.g. "3.4.5" and returns -1, 0 or 1 as
282 * the first version precedes, is equal to, or follows the second
288 public static int compareVersions(String v1, String v2)
290 return compareVersions(v1, v2, null);
294 * Compares two versions formatted as e.g. "3.4.5b1" and returns -1, 0 or 1 as
295 * the first version precedes, is equal to, or follows the second
299 * @param pointSeparator
300 * a string used to delimit point increments in sub-tokens of the
304 public static int compareVersions(String v1, String v2,
305 String pointSeparator)
307 if (v1 == null || v2 == null)
311 String[] toks1 = v1.split("\\.");
312 String[] toks2 = v2.split("\\.");
314 for (; i < toks1.length; i++)
316 if (i >= toks2.length)
323 String tok1 = toks1[i];
324 String tok2 = toks2[i];
325 if (pointSeparator != null)
328 * convert e.g. 5b2 into decimal 5.2 for comparison purposes
330 tok1 = tok1.replace(pointSeparator, ".");
331 tok2 = tok2.replace(pointSeparator, ".");
335 float f1 = Float.valueOf(tok1);
336 float f2 = Float.valueOf(tok2);
337 int comp = Float.compare(f1, f2);
342 } catch (NumberFormatException e)
345 .println("Invalid version format found: " + e.getMessage());
350 if (i < toks2.length)
359 * same length, all tokens match
365 * Converts the string to all lower-case except the first character which is
371 public static String toSentenceCase(String s)
379 return s.toUpperCase();
381 return s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
385 * A helper method that strips off any leading or trailing html and body tags.
386 * If no html tag is found, then also html-encodes angle bracket characters.
391 public static String stripHtmlTags(String text)
397 String tmp2up = text.toUpperCase();
398 int startTag = tmp2up.indexOf("<HTML>");
401 text = text.substring(startTag + 6);
402 tmp2up = tmp2up.substring(startTag + 6);
404 // is omission of "<BODY>" intentional here??
405 int endTag = tmp2up.indexOf("</BODY>");
408 text = text.substring(0, endTag);
409 tmp2up = tmp2up.substring(0, endTag);
411 endTag = tmp2up.indexOf("</HTML>");
414 text = text.substring(0, endTag);
417 if (startTag == -1 && (text.contains("<") || text.contains(">")))
419 text = text.replaceAll("<", "<");
420 text = text.replaceAll(">", ">");