List of usage examples for java.lang StringBuilder length
int length();
From source file:Main.java
/** * Formats a given nonce count as a HTTP header value. The header is * {@link org.restlet.engine.header.HeaderConstants#HEADER_AUTHENTICATION_INFO}. * /* w w w. j a va 2 s .com*/ * @param nonceCount * The given nonce count. * @return The formatted value of the given nonce count. */ public static String formatNonceCount(int nonceCount) { StringBuilder result = new StringBuilder(Integer.toHexString(nonceCount)); while (result.length() < 8) { result.insert(0, '0'); } return result.toString(); }
From source file:Main.java
public static String getMoneyString(int value) { String returnValue = "" + value; StringBuilder builder = new StringBuilder(returnValue); if (returnValue.length() > 2) { builder.insert(builder.length() - 2, "."); builder.insert(0, "$"); } else {//w w w . j a va 2s . c o m builder.insert(0, "$0."); } return builder.toString(); }
From source file:Main.java
public static String formatFromSize(long size) { if (size == -1) return ""; String suffix = " B"; if (size >= 1024) { suffix = " KB"; size /= 1024;//from w ww .j a v a 2 s . com if (size >= 1024) { suffix = " MB"; size /= 1024; } } StringBuilder resultBuffer = new StringBuilder(Long.toString(size)); int commaOffset = resultBuffer.length() - 3; while (commaOffset > 0) { resultBuffer.insert(commaOffset, ','); commaOffset -= 3; } if (suffix != null) resultBuffer.append(suffix); return resultBuffer.toString(); }
From source file:Main.java
public static String toIsoDateFormat(Date date) { TimeZone timeZone = TimeZone.getDefault(); boolean utc = TimeZone.getTimeZone("UTC").equals(timeZone) || TimeZone.getTimeZone("GMT").equals(timeZone); String pattern = utc ? "yyyy-MM-dd'T'HH:mm:ss'Z'" : "yyyy-MM-dd'T'HH:mm:ssZ"; SimpleDateFormat format = new SimpleDateFormat(pattern); format.setTimeZone(timeZone);/*w w w . j a va 2 s .co m*/ StringBuilder buffer = new StringBuilder(format.format(date)); if (!utc) { buffer.insert(buffer.length() - 2, ':'); } return buffer.toString(); }
From source file:Main.java
public static String parseFromMap(Map<String, String> mappy) { StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String> e : mappy.entrySet()) { if (sb.length() > 0) { sb.append(LINE_SEP);/* ww w. jav a 2 s . c o m*/ } sb.append(e.getKey()).append(EQUALS).append(e.getValue()); } return sb.toString(); }
From source file:Main.java
public static String formatSqlValuesCsv(Iterable iterable) { StringBuilder builder = new StringBuilder(); for (Object item : iterable) { if (builder.length() > 0) { builder.append(", "); }/* ww w . j a va2s. com*/ builder.append(formatSqlValue(item)); } return builder.toString(); }
From source file:com.sf.ddao.factory.param.JoinListParameter.java
public static String join(Iterable list) { StringBuilder sb = new StringBuilder(); for (Object item : list) { if (sb.length() > 0) { sb.append(','); }/* w w w .j a v a2 s . c o m*/ if (item instanceof Number) { sb.append(item); } else { sb.append("'").append(item).append("'"); } } return sb.toString(); }
From source file:Main.java
/** * Join collection using custom delimiter * * @param collection A collection to join * @param delimiter Custom join delimiter * @return Joined collection as string/*from w w w. j a v a2s .co m*/ */ public static String join(Collection<?> collection, String delimiter) { if (collection == null) { return null; } StringBuilder builder = new StringBuilder(); for (Object element : collection) { if (builder.length() > 0) { builder.append(delimiter); } builder.append(element != null ? element.toString() : ""); } return builder.toString(); }
From source file:Main.java
/** * Escapes backslashes ('\') with additional backslashes in a given String, returning a new, escaped String. * * @param value String to escape. Cannot be null. * @return escaped String. Never is null. *///w ww . j a v a 2s . c o m public static String escapeBackslashes(String value) { StringBuilder buf = new StringBuilder(value); for (int looper = 0; looper < buf.length(); looper++) { char curr = buf.charAt(looper); char next = 0; if (looper + 1 < buf.length()) next = buf.charAt(looper + 1); if (curr == '\\') { if (next != '\\') { // only if not already escaped buf.insert(looper, '\\'); // escape backslash } looper++; // skip past extra backslash (either the one we added or existing) } } return buf.toString(); }
From source file:Main.java
/** * Converts a string[] into a comma-delimited String. * * Takes care of escaping commas using a backlash * @see com.newland.mtypex.iso.core.jpos.iso.ISOUtil#commaDecode(String) * @param ss string array to be comma encoded * @return comma encoded string/* w w w. j a v a 2 s . co m*/ */ public static String commaEncode(String[] ss) { StringBuilder sb = new StringBuilder(); for (String s : ss) { if (sb.length() > 0) sb.append(','); if (s != null) { for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); switch (c) { case '\\': case ',': sb.append('\\'); break; } sb.append(c); } } } return sb.toString(); }