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.io.UnsupportedEncodingException;
24 import java.net.URLEncoder;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Locale;
29 public class StringUtils
31 private static final char PERCENT = '%';
33 private static final boolean DEBUG = false;
36 * URL encoded characters, indexed by char value
37 * e.g. urlEncodings['='] = urlEncodings[61] = "%3D"
39 private static String[] urlEncodings = new String[255];
42 * Returns a new character array, after inserting characters into the given
46 * the character array to insert into
48 * the 0-based position for insertion
50 * the number of characters to insert
52 * the character to insert
54 public static final char[] insertCharAt(char[] in, int position,
57 char[] tmp = new char[in.length + count];
59 if (position >= in.length)
61 System.arraycopy(in, 0, tmp, 0, in.length);
66 System.arraycopy(in, 0, tmp, 0, position);
76 if (position < in.length)
78 System.arraycopy(in, position, tmp, index, in.length - position);
92 public static final char[] deleteChars(char[] in, int from, int to)
94 if (from >= in.length || from < 0)
103 tmp = new char[from];
104 System.arraycopy(in, 0, tmp, 0, from);
109 tmp = new char[in.length - to + from];
110 System.arraycopy(in, 0, tmp, 0, from);
111 System.arraycopy(in, to, tmp, from, in.length - to);
117 * Returns the last part of 'input' after the last occurrence of 'token'. For
118 * example to extract only the filename from a full path or URL.
122 * a delimiter which must be in regular expression format
125 public static String getLastToken(String input, String token)
135 String[] st = input.split(token);
136 return st[st.length - 1];
140 * Parses the input string into components separated by the delimiter. Unlike
141 * String.split(), this method will ignore occurrences of the delimiter which
142 * are nested within single quotes in name-value pair values, e.g. a='b,c'.
143 * New implementation to avoid Pattern for jalviewjs.
147 * @return elements separated by separator
149 public static String[] separatorListToArray(String input,
153 // these two shouldn't return null (one or two "" respectively)
154 || input.equals("") || input.equals(delimiter))
159 final char escapeChar = '\\';
160 final char quoteChar = '\'';
161 int ilength = input.length();
162 int dlength = delimiter.length();
163 List<String> values = new ArrayList<>();
165 boolean escape = false;
166 boolean inquote = false;
169 for (int i = 0; i < ilength; i++)
171 if (!escape && !inquote && ilength >= i + dlength
172 && input.substring(i, i + dlength).equals(delimiter))
175 values.add(input.substring(start, i));
180 char c = input.charAt(i);
196 // add the last value
197 values.add(input.substring(start, ilength));
199 return values.toArray(new String[values.size()]);
203 * Returns a string which contains the list elements delimited by the
204 * separator. Null items are ignored. If the input is null or has length zero,
205 * a single delimiter is returned.
209 * @return concatenated string
211 public static String arrayToSeparatorList(String[] list, String separator)
213 StringBuffer v = new StringBuffer();
214 if (list != null && list.length > 0)
216 for (int i = 0, iSize = list.length; i < iSize; i++)
224 // TODO - escape any separator values in list[i]
231 .println("Returning '" + separator + "' separated List:\n");
232 jalview.bin.Console.errPrintln(v);
238 jalview.bin.Console.errPrintln(
239 "Returning empty '" + separator + "' separated List\n");
241 return "" + separator;
245 * Converts a list to a string with a delimiter before each term except the
246 * first. Returns an empty string given a null or zero-length argument. This
247 * can be replaced with StringJoiner in Java 8.
253 public static String listToDelimitedString(List<String> terms,
256 StringBuilder sb = new StringBuilder(32);
257 if (terms != null && !terms.isEmpty())
259 boolean appended = false;
260 for (String term : terms)
270 return sb.toString();
274 * Convenience method to parse a string to an integer, returning 0 if the
275 * input is null or not a valid integer
280 public static int parseInt(String s)
283 if (s != null && s.length() > 0)
287 result = Integer.parseInt(s);
288 } catch (NumberFormatException ex)
296 * Compares two versions formatted as e.g. "3.4.5" and returns -1, 0 or 1 as
297 * the first version precedes, is equal to, or follows the second
303 public static int compareVersions(String v1, String v2)
305 return compareVersions(v1, v2, null);
309 * Compares two versions formatted as e.g. "3.4.5b1" and returns -1, 0 or 1 as
310 * the first version precedes, is equal to, or follows the second
314 * @param pointSeparator
315 * a string used to delimit point increments in sub-tokens of the
319 public static int compareVersions(String v1, String v2,
320 String pointSeparator)
322 if (v1 == null || v2 == null)
326 String[] toks1 = v1.split("\\.");
327 String[] toks2 = v2.split("\\.");
329 for (; i < toks1.length; i++)
331 if (i >= toks2.length)
338 String tok1 = toks1[i];
339 String tok2 = toks2[i];
340 if (pointSeparator != null)
343 * convert e.g. 5b2 into decimal 5.2 for comparison purposes
345 tok1 = tok1.replace(pointSeparator, ".");
346 tok2 = tok2.replace(pointSeparator, ".");
350 float f1 = Float.valueOf(tok1);
351 float f2 = Float.valueOf(tok2);
352 int comp = Float.compare(f1, f2);
357 } catch (NumberFormatException e)
360 .println("Invalid version format found: " + e.getMessage());
365 if (i < toks2.length)
374 * same length, all tokens match
380 * Converts the string to all lower-case except the first character which is
386 public static String toSentenceCase(String s)
394 return s.toUpperCase(Locale.ROOT);
396 return s.substring(0, 1).toUpperCase(Locale.ROOT)
397 + s.substring(1).toLowerCase(Locale.ROOT);
401 * A helper method that strips off any leading or trailing html and body tags.
402 * If no html tag is found, then also html-encodes angle bracket characters.
407 public static String stripHtmlTags(String text)
413 String tmp2up = text.toUpperCase(Locale.ROOT);
414 int startTag = tmp2up.indexOf("<HTML>");
417 text = text.substring(startTag + 6);
418 tmp2up = tmp2up.substring(startTag + 6);
420 // is omission of "<BODY>" intentional here??
421 int endTag = tmp2up.indexOf("</BODY>");
424 text = text.substring(0, endTag);
425 tmp2up = tmp2up.substring(0, endTag);
427 endTag = tmp2up.indexOf("</HTML>");
430 text = text.substring(0, endTag);
433 if (startTag == -1 && (text.contains("<") || text.contains(">")))
435 text = text.replaceAll("<", "<");
436 text = text.replaceAll(">", ">");
442 * Answers the input string with any occurrences of the 'encodeable'
443 * characters replaced by their URL encoding
449 public static String urlEncode(String s, String encodable)
451 if (s == null || s.isEmpty())
457 * do % encoding first, as otherwise it may double-encode!
459 if (encodable.indexOf(PERCENT) != -1)
461 s = urlEncode(s, PERCENT);
464 for (char c : encodable.toCharArray())
475 * Answers the input string with any occurrences of {@code c} replaced with
476 * their url encoding. Answers the input string if it is unchanged.
482 static String urlEncode(String s, char c)
484 String decoded = String.valueOf(c);
485 if (s.indexOf(decoded) != -1)
487 String encoded = getUrlEncoding(c);
488 if (!encoded.equals(decoded))
490 s = s.replace(decoded, encoded);
497 * Answers the input string with any occurrences of the specified (unencoded)
498 * characters replaced by their URL decoding.
500 * Example: {@code urlDecode("a%3Db%3Bc", "-;=,")} should answer
507 public static String urlDecode(String s, String encodable)
509 if (s == null || s.isEmpty())
514 for (char c : encodable.toCharArray())
516 String encoded = getUrlEncoding(c);
517 if (s.indexOf(encoded) != -1)
519 String decoded = String.valueOf(c);
520 s = s.replace(encoded, decoded);
527 * Does a lazy lookup of the url encoding of the given character, saving the
528 * value for repeat lookups
533 private static String getUrlEncoding(char c)
535 if (c < 0 || c >= urlEncodings.length)
537 return String.valueOf(c);
540 String enc = urlEncodings[c];
545 enc = urlEncodings[c] = URLEncoder.encode(String.valueOf(c),
547 } catch (UnsupportedEncodingException e)
549 enc = urlEncodings[c] = String.valueOf(c);
555 public static int firstCharPosIgnoreCase(String text, String chars)
557 int min = text.length() + 1;
558 for (char c : chars.toLowerCase(Locale.ROOT).toCharArray())
560 int i = text.toLowerCase(Locale.ROOT).indexOf(c);
561 if (0 <= i && i < min)
566 return min < text.length() + 1 ? min : -1;
569 public static boolean equalsIgnoreCase(String s1, String s2)
571 if (s1 == null || s2 == null)
575 return s1.toLowerCase(Locale.ROOT).equals(s2.toLowerCase(Locale.ROOT));
578 public static int indexOfFirstWhitespace(String text)
580 // Rewritten to not use regex for Jalviewjs. Probably more efficient this
586 for (int i = 0; i < text.length(); i++)
588 if (Character.isWhitespace(text.charAt(i)))
597 * implementation of String.replaceLast.
598 * Replaces only the last occurrence of toReplace in string with replacement.
600 public static String replaceLast(String string, String toReplace,
603 int pos = string.lastIndexOf(toReplace);
606 return new StringBuilder().append(string.substring(0, pos))
608 .append(string.substring(pos + toReplace.length()))
619 * return the maximum length of a List of Strings
621 public static int maxLength(List<String> l)
628 if (s.length() > max)