List of usage examples for java.lang StringBuilder length
int length();
From source file:Main.java
/** * Adds an attribute with the list of integer values encoded as a string. * @param element the element to which the attribute will be added * @param attributeName the name of the attribute * @param intValues the list of Integer values to set *//*from ww w . ja v a 2s. com*/ public static void setIntegerListAttribute(Element element, String attributeName, List<Integer> intValues) { StringBuilder sb = new StringBuilder(); for (Integer intValue : intValues) { if (sb.length() == 0) { sb.append(" "); //$NON-NLS-1$ } sb.append(intValue); } element.setAttribute(attributeName, sb.toString()); }
From source file:Main.java
public static <T> String join(List<T> array, String separator) { if (array == null || separator == null) { return ""; }// www.j a va 2 s . co m StringBuilder result = new StringBuilder(); for (T item : array) { if (result.length() > 0) { result.append(separator).append(String.valueOf(item)); } else { result.append(String.valueOf(item)); } } return result.toString(); }
From source file:Main.java
public static String formatValue(long value) throws NumberFormatException { if (value < 0) { throw new NumberFormatException("Negative value " + value); }// w ww .j a va 2 s .c o m StringBuilder sb = new StringBuilder(Long.toString(value)); while (sb.length() <= 8) { sb.insert(0, '0'); } sb.insert(sb.length() - 8, '.'); while (sb.length() > 1 && (sb.charAt(sb.length() - 1) == '0' || sb.charAt(sb.length() - 1) == '.')) { sb.setLength(sb.length() - 1); } return sb.toString(); }
From source file:Main.java
public static String encodeMultiIntegerField(int[] values) { StringBuilder encodedField = new StringBuilder(); for (int value : values) { if (encodedField.length() > 0) { encodedField.append(';'); }/* www.j a v a2 s . c o m*/ encodedField.append(value); } return encodedField.toString(); }
From source file:Main.java
public static String toMatchString(List<String> terms) { StringBuilder builder = new StringBuilder(); for (String term : terms) { if (builder.length() != 0) { builder.append(' '); }/*from w ww. j a va2 s .co m*/ if (isKeyword(term)) { builder.append(term.toUpperCase(Locale.ENGLISH)); } else if (term.contains("*") || term.startsWith("-")) { builder.append(term); } else { builder.append('*').append(term).append('*'); } } return builder.toString(); }
From source file:Main.java
public static String getCauses(Throwable e) { try {/*from w w w . j a va 2 s . co m*/ StringBuilder sb = new StringBuilder(); while (e != null) { if (sb.length() > 0) { sb.append(", "); } sb.append(e.getClass().getSimpleName()); e = e.getCause(); } return sb.toString(); } catch (Throwable derp) { return "derp " + derp.getClass().getSimpleName(); } }
From source file:Main.java
/** * Converts a string to title casing.//from w ww . jav a2s . c o m * @param str * The string to convert. * @return * The converted string. */ public static String toTitleCase(String str) { if (str == null) { return null; } // skip if already contains mixed case if (!str.equals(str.toLowerCase()) && !str.equals(str.toUpperCase())) { return str; } boolean isSeparator = true; StringBuilder builder = new StringBuilder(str); final int len = builder.length(); for (int i = 0; i < len; ++i) { char c = builder.charAt(i); if (isSeparator) { if (Character.isLetterOrDigit(c)) { // Convert to title case and switch out of whitespace mode. builder.setCharAt(i, Character.toTitleCase(c)); isSeparator = false; } } else if (!Character.isLetterOrDigit(c)) { isSeparator = true; } else { builder.setCharAt(i, Character.toLowerCase(c)); } } return builder.toString(); }
From source file:Main.java
public static String encodeMultiStringField(String[] values) { StringBuilder encodedField = new StringBuilder(); for (String value : values) { if (encodedField.length() > 0) { encodedField.append(';'); }//from w ww.j ava2 s . c om encodedField.append(value); } return encodedField.toString(); }
From source file:Main.java
public static String formatSize(long size) { String suffix = null;/*w w w. j a v a 2 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
/** * Converts a string to title casing./*from w w w .j ava2 s .co m*/ * * @param str The string to convert. * @return The converted string. */ public static String toTitleCase(String str) { if (str == null) { return null; } boolean isSeparator = true; StringBuilder builder = new StringBuilder(str); final int len = builder.length(); for (int i = 0; i < len; ++i) { char c = builder.charAt(i); if (isSeparator) { if (Character.isLetterOrDigit(c)) { // Convert to title case and switch out of whitespace mode. builder.setCharAt(i, Character.toTitleCase(c)); isSeparator = false; } } else if (!Character.isLetterOrDigit(c)) { isSeparator = true; } else { builder.setCharAt(i, Character.toLowerCase(c)); } } return builder.toString(); }