/** * Concatenates strings, using whitespaces as separator. * * @param strings Strings to join. */ public static String join(Iterable<String> strings) { return join(" ", strings); }
/** Same as byteToHexString(bytes, 0, bytes.length). */ public static String byteToHexString(byte bytes[]) { return byteToHexString(bytes, 0, bytes.length); }
/** * Escape <code>charToEscape</code> in the string * with the escape char <code>escapeChar</code> * * @param str string * @param escapeChar escape char * @param charToEscape the char to be escaped * @return an escaped string */ public static String escapeString(String str, char escapeChar, char charToEscape) { return escapeString(str, escapeChar, new char[] {charToEscape}); }
/** * @param charsToEscape array of characters to be escaped */ public static String escapeString(String str, char escapeChar, char[] charsToEscape) { if (str == null) { return null; } StringBuilder result = new StringBuilder(); for (int i = 0; i < str.length(); i++) { char curChar = str.charAt(i); if (curChar == escapeChar || hasChar(charsToEscape, curChar)) { // special char result.append(escapeChar); } result.append(curChar); } return result.toString(); }
/** * * Given a finish and start time in long milliseconds, returns a * String in the format Xhrs, Ymins, Z sec, for the time difference between two times. * If finish time comes before start time then negative valeus of X, Y and Z wil return. * * @param finishTime finish time * @param startTime start time */ public static String formatTimeDiff(long finishTime, long startTime) { long timeDiff = finishTime - startTime; return formatTime(timeDiff); }
/** * Splits a comma separated value <code>String</code>, trimming leading and trailing whitespace on each value. * @param str a comma separated <String> with values * @return a <code>Collection</code> of <code>String</code> values */ public static Collection<String> getTrimmedStringCollection(String str) { return Arrays.asList(getTrimmedStrings(str)); }
/** * Return an abbreviated English-language desc of the byte length */ public static String byteDesc(long len) { double val; String ending; if (len < 1024 * 1024) { val = (1.0 * len) / 1024; ending = " KB"; } else if (len < 1024 * 1024 * 1024) { val = (1.0 * len) / (1024 * 1024); ending = " MB"; } else if (len < 1024L * 1024 * 1024 * 1024) { val = (1.0 * len) / (1024 * 1024 * 1024); ending = " GB"; } else if (len < 1024L * 1024 * 1024 * 1024 * 1024) { val = (1.0 * len) / (1024L * 1024 * 1024 * 1024); ending = " TB"; } else { val = (1.0 * len) / (1024L * 1024 * 1024 * 1024 * 1024); ending = " PB"; } return limitDecimalTo2(val) + ending; }
/** * Returns an arraylist of strings. * @param str the comma separated string values * @return the arraylist of the comma separated string values */ public static String[] getStrings(String str) { Collection<String> values = getStringCollection(str); if (values.isEmpty()) { return null; } return values.toArray(new String[values.size()]); }
/** * Formats time in ms and appends difference (finishTime - startTime) * as returned by formatTimeDiff(). * If finish time is 0, empty string is returned, if start time is 0 * then difference is not appended to return value. * @param dateFormat date format to use * @param finishTime fnish time * @param startTime start time * @return formatted value. */ public static String getFormattedTimeWithDiff(DateFormat dateFormat, long finishTime, long startTime) { StringBuilder buf = new StringBuilder(); if (0 != finishTime) { buf.append(dateFormat.format(new Date(finishTime))); if (0 != startTime) { buf.append(" (" + formatTimeDiff(finishTime, startTime) + ")"); } } return buf.toString(); }
/** * Split a string using the given separator * @param str a string that may have escaped separator * @param escapeChar a char that be used to escape the separator * @param separator a separator char * @return an array of strings */ public static String[] split(String str, char escapeChar, char separator) { if (str == null) { return null; } ArrayList<String> strList = new ArrayList<>(); StringBuilder split = new StringBuilder(); int index = 0; while ((index = findNext(str, separator, escapeChar, index, split)) >= 0) { ++index; // move over the separator for next search strList.add(split.toString()); split.setLength(0); // reset the buffer } strList.add(split.toString()); // remove trailing empty split(s) int last = strList.size(); // last split while (--last >= 0 && "".equals(strList.get(last))) { strList.remove(last); } return strList.toArray(new String[strList.size()]); }
String attributeJoinStr = StringUtils.join("/", attributeStrParts); // FIXME: what if attributeJoinStr is null or empty ???
/** * Escape commas in the string using the default escape char * @param str a string * @return an escaped string */ public static String escapeString(String str) { return escapeString(str, ESCAPE_CHAR, COMMA); }