/** * Helper function for making empty strings into a null. * * @return null if s is zero length. otherwise, returns s. */ public static String toNullIfEmpty(String s) { return (StringUtil.isEmpty(s)) ? null : s; }
/** * Lots of people called list2String when in fact it was implemented as * Collection2String. I added Collection2String as a new function and am * leaving the list2String function signature here so it can continue to be * * @deprecated Please use * But note that {@code Join} does not consider null elements to be * equivalent to the empty string, as this method does. */ @Deprecated public static String list2String( Collection<?> in, String separator) { return Collection2String(in, separator); }
/** * Convert a byte array to a String using UTF-8 encoding. * * @return the decoded String or null if ba is null */ public static String bytesToUtf8(final byte[] ba) { // UTF-8 should always be supported return bytesToEncoding(ba, "UTF8"); }
/** Strip white spaces from both end, and collapse white spaces * in the middle. * @param str what to strip * @return String the striped and collapsed string */ public static String stripAndCollapse(String str) { return collapseWhitespace(strip(str)); }
if (isEmpty(delimEntry) || isEmpty(delimKey)) { out.put(strip(in), ""); return out; Iterator<String> it = string2List(in, delimEntry, false).iterator(); int len = delimKey.length(); while (it.hasNext()) { String value = entry.substring(pos + len); if (doStripEntry) { value = strip(value); out.put(strip(entry.substring(0, pos)), value); } else { out.put(strip(entry), "");
/** * The old interface to cropBetween - using a single char limit */ public static String cropBetween(String in, char limit) { return cropBetween(in, String.valueOf(new char[]{limit})); }
/** * Convert a byte array to a hex-encoding string: "a33bff00..." */ public static String bytesToHexString(final byte[] bytes) { return bytesToHexString(bytes, null); }
/** * Replaces any string of adjacent whitespace characters with the whitespace * character " ". * * @param str the string you want to munge * @return String with no more excessive whitespace! * * @see collapse */ public static String collapseWhitespace(String str) { return collapse(str, WHITE_SPACES, " "); }
/** * This concatenates the elements of a collection in a string * * @param in - the collection that has to be conatenated * @param separator - a string to sepparate the elements from the list * * @return String * * @deprecated Please use * But note that {@code Join} does not consider null elements to be * equivalent to the empty string, as this method does. */ @Deprecated public static String Collection2String( Collection<?> in, String separator) { if (in == null) { return null; } return Iterator2String(in.iterator(), separator); }
/** * Replaces each non-ascii character in s with its Unicode escape sequence * \\uxxxx where xxxx is a hex number. Existing escape sequences won't be * affected. */ public static String unicodeEscape(String s) { if (allAscii(s)) { return s; } StringBuilder sb = new StringBuilder(s.length()); int len = s.length(); for (int i = 0; i < len; ++i) { char ch = s.charAt(i); if (ch <= 127) { sb.append(ch); } else { sb.append("\\u"); String hexString = Integer.toHexString(ch); // Pad with zeros if necessary int numZerosToPad = 4- hexString.length(); for (int j = 0; j < numZerosToPad; ++j) { sb.append('0'); } sb.append(hexString); } } return sb.toString(); }
case '=': appendHexJavaScriptRepresentation(sb, c); break; appendHexJavaScriptRepresentation(sb, c); } else { sb.append(c); default: if (c >= 128 && escapeToAscii) { appendHexJavaScriptRepresentation(sb, c); } else { sb.append(c);
if (isEmpty(delimEntry) || isEmpty(delimKey)) { out.put(strip(in), ""); return out; Iterator<String> it = string2List(in, delimEntry, false).iterator(); int len = delimKey.length(); while (it.hasNext()) { String value = entry.substring(pos + len); if (doStripEntry) { value = strip(value); out.put(strip(entry.substring(0, pos)), value); } else { out.put(strip(entry), "");
/** Strip white spaces from both end, and collapse white spaces * in the middle. * @param str what to strip * @return String the striped and collapsed string */ public static String stripAndCollapse(String str) { return collapseWhitespace(strip(str)); }
/** * The old interface to cropBetween - using a single char limit */ public static String cropBetween(String in, char limit) { return cropBetween(in, String.valueOf(new char[]{limit})); }
/** * Convert a byte array to a hex-encoding string: "a33bff00..." */ public static String bytesToHexString(final byte[] bytes) { return bytesToHexString(bytes, null); }
/** * Replaces any string of adjacent whitespace characters with the whitespace * character " ". * * @param str the string you want to munge * @return String with no more excessive whitespace! * * @see collapse */ public static String collapseWhitespace(String str) { return collapse(str, WHITE_SPACES, " "); }
/** * This concatenates the elements of a collection in a string * * @param in - the collection that has to be conatenated * @param separator - a string to sepparate the elements from the list * * @return String * * @deprecated Please use * But note that {@code Join} does not consider null elements to be * equivalent to the empty string, as this method does. */ @Deprecated public static String Collection2String( Collection<?> in, String separator) { if (in == null) { return null; } return Iterator2String(in.iterator(), separator); }
/** * Replaces each non-ascii character in s with its Unicode escape sequence * \\uxxxx where xxxx is a hex number. Existing escape sequences won't be * affected. */ public static String unicodeEscape(String s) { if (allAscii(s)) { return s; } StringBuilder sb = new StringBuilder(s.length()); int len = s.length(); for (int i = 0; i < len; ++i) { char ch = s.charAt(i); if (ch <= 127) { sb.append(ch); } else { sb.append("\\u"); String hexString = Integer.toHexString(ch); // Pad with zeros if necessary int numZerosToPad = 4- hexString.length(); for (int j = 0; j < numZerosToPad; ++j) { sb.append('0'); } sb.append(hexString); } } return sb.toString(); }
case '=': appendHexJavaScriptRepresentation(sb, c); break; appendHexJavaScriptRepresentation(sb, c); } else { sb.append(c); default: if (c >= 128 && escapeToAscii) { appendHexJavaScriptRepresentation(sb, c); } else { sb.append(c);
if (isEmpty(delimEntry) || isEmpty(delimKey)) { out.put(strip(in), ""); return out; Iterator<String> it = string2List(in, delimEntry, false).iterator(); int len = delimKey.length(); while (it.hasNext()) { String value = entry.substring(pos + len); if (doStripEntry) { value = strip(value); out.put(strip(entry.substring(0, pos)), value); } else { out.put(strip(entry), "");