List of usage examples for java.lang StringBuilder length
int length();
From source file:Main.java
public static StringBuilder getThreadSafeStringBuilder() { StringBuilder sb = threadSafeStrBuilder.get(); if (sb == null) { sb = new StringBuilder(); threadSafeStrBuilder.set(sb);/* w w w .j ava 2 s. co m*/ } sb.delete(0, sb.length()); return sb; }
From source file:com.norconex.collector.core.pipeline.importer.SaveDocumentStage.java
private static String[] splitLargeSegment(String segment) { if (segment.length() <= MAX_SEGMENT_LENGTH) { return new String[] { segment }; }//from w w w.j a v a 2 s .c o m List<String> segments = new ArrayList<>(); StringBuilder b = new StringBuilder(segment); while (b.length() > MAX_SEGMENT_LENGTH) { segments.add(b.substring(0, MAX_SEGMENT_LENGTH)); b.delete(0, MAX_SEGMENT_LENGTH); } segments.add(b.substring(0)); return segments.toArray(ArrayUtils.EMPTY_STRING_ARRAY); }
From source file:Main.java
/** * Get a hexadecimal representation of a byte array starting at <code>offset</code> index for <code>len</code> * bytes, with each octet separated by a space. * * @param array/*from w ww . j a va 2 s . com*/ * @param offset * @param len * * @return hex string, each octet delimited by a space */ public static String printHex(byte[] array, int offset, int len) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i++) { byte b = array[offset + i]; if (sb.length() > 0) sb.append(' '); sb.append(digits[b >> 4 & 0x0F]); sb.append(digits[b & 0x0F]); } return sb.toString(); }
From source file:Main.java
public static String zeropadRight(String s, int len) { StringBuilder d = new StringBuilder(s); while (d.length() < len) d.append('0'); return d.toString(); }
From source file:Main.java
public static String formatSize(long size) { String suffix = null;//from ww w . j ava2 s .c om if (size >= 1024) { suffix = "KB"; size /= 1024; 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 <T> String concat(Collection<T> col, String separator) { StringBuilder result = new StringBuilder(); for (T e : col) { String s = e.toString();// ww w . j a v a 2s . co m if (!s.isEmpty()) { if (result.length() > 0) result.append(separator); result.append(s); } } return result.toString(); }
From source file:Main.java
/** Concatenates any number of clauses using the specified operation. */ public static String concatenateClausesWithOperation(String operation, String... clauses) { // Nothing to concatenate. if (clauses.length == 1) { return clauses[0]; }/*from w w w. j av a 2 s .c om*/ StringBuilder builder = new StringBuilder(); for (String clause : clauses) { if (!TextUtils.isEmpty(clause)) { if (builder.length() > 0) { builder.append(" ").append(operation).append(" "); } builder.append("("); builder.append(clause); builder.append(")"); } } return builder.toString(); }
From source file:Main.java
public static String urlWithParameters(String url, Map<String, Object> params) { StringBuilder builder = new StringBuilder(); if (params != null) { for (Map.Entry<String, Object> entry : params.entrySet()) { if (builder.length() == 0) { if (url.contains("?")) { builder.append("&"); } else { builder.append("?"); }/*w w w . j a v a 2s . c o m*/ } else { builder.append("&"); } builder.append(entry.getKey()).append("=").append(entry.getValue()); } } return url + builder.toString(); }
From source file:Main.java
public static void print(int[] objs) { StringBuilder builder = new StringBuilder(); for (int e : objs) { builder.append("," + e); }/* w ww. j av a 2 s . c o m*/ System.out.println(builder.substring(1, builder.length())); }
From source file:Main.java
public static String getMethodName(Method method, Class<?>[] parameterClasses, String rightCode) { if (method.getParameterTypes().length > parameterClasses.length) { Class<?>[] types = method.getParameterTypes(); StringBuilder buf = new StringBuilder(rightCode); for (int i = parameterClasses.length; i < types.length; i++) { if (buf.length() > 0) { buf.append(","); }/* w w w .j a v a 2 s . c o m*/ Class<?> type = types[i]; String def; if (type == boolean.class) { def = "false"; } else if (type == char.class) { def = "\'\\0\'"; } else if (type == byte.class || type == short.class || type == int.class || type == long.class || type == float.class || type == double.class) { def = "0"; } else { def = "null"; } buf.append(def); } } return method.getName() + "(" + rightCode + ")"; }