X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Futil%2FStringUtils.java;h=6044655b80c327eb3a237d82d72a6e46156284da;hb=449fb0be731c9283e9cad076a03fe730aeb2095a;hp=533e98beabeb205e6f27f8cd866da9d1ad718768;hpb=db4eacee27b836db4126dca551887bfc6652d72a;p=jalview.git diff --git a/src/jalview/util/StringUtils.java b/src/jalview/util/StringUtils.java index 533e98b..6044655 100644 --- a/src/jalview/util/StringUtils.java +++ b/src/jalview/util/StringUtils.java @@ -248,4 +248,55 @@ public class StringUtils } return "" + separator; } + + /** + * Converts a list to a string with a delimiter before each term except the + * first. Returns an empty string given a null or zero-length argument. This + * can be replaced with StringJoiner in Java 8. + * + * @param terms + * @param delim + * @return + */ + public static String listToDelimitedString(List terms, + String delim) + { + StringBuilder sb = new StringBuilder(32); + if (terms != null && !terms.isEmpty()) + { + boolean appended = false; + for (String term : terms) + { + if (appended) + { + sb.append(delim); + } + appended = true; + sb.append(term); + } + } + return sb.toString(); + } + + /** + * Convenience method to parse a string to an integer, returning 0 if the + * input is null or not a valid integer + * + * @param s + * @return + */ + public static int parseInt(String s) + { + int result = 0; + if (s != null && s.length() > 0) + { + try + { + result = Integer.parseInt(s); + } catch (NumberFormatException ex) + { + } + } + return result; + } }