List of usage examples for java.lang StringBuilder length
int length();
From source file:Main.java
public static String map2String(Map<String, String> map) { StringBuilder result = new StringBuilder(); if (map == null || map.size() == 0) return ""; for (String key : map.keySet()) { result.append(key).append("=").append(map.get(key)).append("&"); }/*from w w w .j av a2s . c o m*/ return result.substring(0, result.length() - 1); }
From source file:StringUtils.java
/** * Reformats a string where lines that are longer than <tt>width</tt> * are split apart at the earliest wordbreak or at maxLength, whichever is * sooner. If the width specified is less than 5 or greater than the input * Strings length the string will be returned as is. * <p/>/*from www .j ava 2 s .co m*/ * Please note that this method can be lossy - trailing spaces on wrapped * lines may be trimmed. * * @param input the String to reformat. * @param width the maximum length of any one line. * @return a new String with reformatted as needed. */ public static String wordWrap(String input, int width, Locale locale) { // protect ourselves if (input == null) { return ""; } else if (width < 5) { return input; } else if (width >= input.length()) { return input; } StringBuilder buf = new StringBuilder(input); boolean endOfLine = false; int lineStart = 0; for (int i = 0; i < buf.length(); i++) { if (buf.charAt(i) == '\n') { lineStart = i + 1; endOfLine = true; } // handle splitting at width character if (i > lineStart + width - 1) { if (!endOfLine) { int limit = i - lineStart - 1; BreakIterator breaks = BreakIterator.getLineInstance(locale); breaks.setText(buf.substring(lineStart, i)); int end = breaks.last(); // if the last character in the search string isn't a space, // we can't split on it (looks bad). Search for a previous // break character if (end == limit + 1) { if (!Character.isWhitespace(buf.charAt(lineStart + end))) { end = breaks.preceding(end - 1); } } // if the last character is a space, replace it with a \n if (end != BreakIterator.DONE && end == limit + 1) { buf.replace(lineStart + end, lineStart + end + 1, "\n"); lineStart = lineStart + end; } // otherwise, just insert a \n else if (end != BreakIterator.DONE && end != 0) { buf.insert(lineStart + end, '\n'); lineStart = lineStart + end + 1; } else { buf.insert(i, '\n'); lineStart = i + 1; } } else { buf.insert(i, '\n'); lineStart = i + 1; endOfLine = false; } } } return buf.toString(); }
From source file:com.google.jenkins.flakyTestHandler.plugin.TestFlakyStatsOverRevision.java
/** * Get the name of a test that's URL-safe. * * @param testName input test name/*from w w w.j av a 2s . com*/ * @return name of the test with illegal characters replaced with '_' */ public static String getSafeTestName(String testName) { StringBuilder buf = new StringBuilder(testName); for (int i = 0; i < buf.length(); i++) { char ch = buf.charAt(i); if (!Character.isJavaIdentifierPart(ch)) { buf.setCharAt(i, '_'); } } return buf.toString(); }
From source file:examples.mail.IMAPImportMbox.java
private static boolean process(final StringBuilder sb, final IMAPClient imap, final String folder, final int msgNum) throws IOException { final int length = sb.length(); boolean haveMessage = length > 2; if (haveMessage) { System.out.println("MsgNum: " + msgNum + " Length " + length); sb.setLength(length - 2); // drop trailing CRLF String msg = sb.toString(); if (!imap.append(folder, null, null, msg)) { throw new IOException("Failed to import message: " + msgNum + " " + imap.getReplyString()); }/* w ww. j a v a2 s .co m*/ } return haveMessage; }
From source file:org.cleverbus.core.common.exception.ExceptionTranslator.java
/** * Returns recursively error messages from the whole exception hierarchy. * * @param ex the exception//from w w w .j a va 2 s . c om * @return exception message */ private static String getMessagesInExceptionHierarchy(Throwable ex) { Assert.notNull(ex, "the ex must not be null"); // use a util that can handle recursive cause structure: Throwable[] hierarchy = ExceptionUtils.getThrowables(ex); StringBuilder messages = new StringBuilder(); for (Throwable throwable : hierarchy) { if (messages.length() > 0) { messages.append(" => "); } messages.append(throwable.getClass().getSimpleName()).append(": ").append(throwable.getMessage()); } return messages.toString(); }
From source file:gov.bnl.channelfinder.api.JSONChannels.java
/** * Creates a compact string representation for the log. * * @param data JSONChannel to create the string representation for * @return string representation/*from w w w . ja va2s . co m*/ */ public static String toLog(JSONChannels data) { if (data.getChannels().size() == 0) { return "[None]"; } else { StringBuilder s = new StringBuilder(); s.append("["); for (JSONChannel c : data.getChannels()) { s.append(JSONChannel.toLog(c) + ","); } s.delete(s.length() - 1, s.length()); s.append("]"); return s.toString(); } }
From source file:camp.pixels.signage.util.NetworkInterfaces.java
public static String getMACAddress(String interfaceName) { try {// w w w .j av a 2 s . c o m List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { if (interfaceName != null) { if (!intf.getName().equalsIgnoreCase(interfaceName)) continue; } if (!VALID_INTERFACES.contains(intf.getName())) continue; byte[] mac = intf.getHardwareAddress(); if (mac == null) continue;//return ""; StringBuilder buf = new StringBuilder(); for (int idx = 0; idx < mac.length; idx++) buf.append(String.format("%02X:", mac[idx])); if (buf.length() > 0) buf.deleteCharAt(buf.length() - 1); //Log.d("getMACAddress", intf.getName()); //Log.d("getMACAddress", buf.toString()); return buf.toString(); } } catch (SocketException ex) { } return ""; }
From source file:Main.java
/** * Null-safe method for writing the items of a string array out as a string * separated by the given char separator. * * @param array the array.//from w w w.jav a 2 s. c o m * @param separator the separator of the array items. * @return a string. */ public static String toString(String[] array, String separator) { StringBuilder builder = new StringBuilder(); if (array != null && array.length > 0) { for (String string : array) { builder.append(string).append(separator); } builder.deleteCharAt(builder.length() - 1); } return builder.toString(); }
From source file:com.pedra.storefront.util.MetaSanitizerUtil.java
/** * Takes a List of KeywordModels and returns a comma separated list of keywords as String. * //from w w w .j a v a 2 s .c om * @param keywords * List of KeywordModel objects * @return String of comma separated keywords */ public static String sanitizeKeywords(final List<KeywordModel> keywords) { if (keywords != null && !keywords.isEmpty()) { // Remove duplicates final Set<String> keywordSet = new HashSet<String>(keywords.size()); for (final KeywordModel kw : keywords) { keywordSet.add(kw.getKeyword()); } // Format keywords, join with comma final StringBuilder stringBuilder = new StringBuilder(); for (final String kw : keywordSet) { stringBuilder.append(kw).append(','); } if (stringBuilder.length() > 0) { // Remove last comma return stringBuilder.substring(0, stringBuilder.length() - 1); } } return ""; }
From source file:io.cloudslang.content.httpclient.build.conn.ConnectionManagerBuilder.java
public static String buildConnectionManagerMapKey(String... connectionManagerMapKeys) { StringBuilder keyBuilder = new StringBuilder(); for (String token : connectionManagerMapKeys) { keyBuilder.append(token).append(":"); }//from w w w . j a va 2 s. c om if (keyBuilder.length() > 0) { keyBuilder.deleteCharAt(keyBuilder.length() - 1); } return keyBuilder.toString(); }