List of usage examples for java.lang StringBuilder length
int length();
From source file:au.org.ala.layers.util.BatchProducer.java
private static String readFile(String filename) throws IOException { BufferedReader br = new BufferedReader(new FileReader(filename)); StringBuilder sb = new StringBuilder(); String line;/*from w ww . j av a 2 s. c om*/ while ((line = br.readLine()) != null) { if (sb.length() > 0) { sb.append("\n"); } sb.append(line); } return sb.toString(); }
From source file:Main.java
/** * Get the host name by mesh ip/*from w ww . j a va2 s. co m*/ * * @param meshIp the mesh ip, e.g. C0A80102 * @return the ip address, e.g. 192.168.1.2 */ public static String getHostNameByMeshIp(String meshIp) { StringBuilder sb = new StringBuilder(); int value; for (int i = 0; i < meshIp.length(); i += 2) { value = Integer.parseInt(meshIp.substring(i, i + 2), 16); sb.append(value); sb.append("."); } return sb.substring(0, sb.length() - 1); }
From source file:Main.java
private static String __getNextWord(String s, int pos) { int i = 0;/* w w w .j a v a 2 s .com*/ StringBuilder sb = new StringBuilder(); while (pos + i < s.length()) { char c = s.charAt(pos + i); if (Character.isLetter(c)) { sb.append(c); } else { break; } i++; } if (sb.length() == 0) return null; return sb.toString(); }
From source file:Main.java
public static String getParams(ExecutableElement d) { Collection<? extends VariableElement> params = d.getParameters(); if (params.size() == 0) return ""; StringBuilder sbuf = new StringBuilder(params.size() * 20); for (VariableElement param : params) { sbuf.append(param.asType());//w ww .java 2s .c o m sbuf.append(' '); sbuf.append(param.getSimpleName()); sbuf.append(','); } sbuf.setLength(sbuf.length() - 1); return sbuf.toString(); }
From source file:com.github.xbn.text.CharUtil.java
/** <p>XXX</p>/*from w w w . j a v a 2 s. c om*/ */ public static final StringBuilder appendDuped(StringBuilder to_appendTo, char chr, int total_dups) { if (total_dups < 1) { return to_appendTo; } try { to_appendTo.setLength(to_appendTo.length() + total_dups); } catch (RuntimeException rx) { throw CrashIfObject.nullOrReturnCause(to_appendTo, "to_appendTo", null, rx); } for (int i = 0; i < total_dups; i++) { to_appendTo.append(chr); } return to_appendTo; }
From source file:io.takari.maven.testing.executor.ForkedLauncher.java
private static String toPath(List<String> strings) { StringBuilder sb = new StringBuilder(); for (String string : strings) { if (sb.length() > 0) { sb.append(File.pathSeparatorChar); }//from w w w .j ava2 s.co m sb.append(string); } return sb.toString(); }
From source file:io.lavagna.model.util.ShortNameGenerator.java
private static String takeFirstFourUpperCaseChars(String s) { StringBuilder sb = new StringBuilder(); for (char c : s.toCharArray()) { if (Character.isUpperCase(c)) { sb.append(c);// w ww. ja v a2s.c o m } } return sb.substring(0, Math.min(sb.length(), 4)); }
From source file:Main.java
/** * Returns the XPath to retrieve targetElement from rootElement. rootElement may be null, in this case the XPath starts with and includes * the farthest non-null ancestor of targetElement. If rootElement == targetElement, an empty string * is returned. //ww w. j av a2 s . c o m * @param includeElementIndex Indicates if the element indices in the form elementName[n] should * be included in the XPath. * @param namespacesMap Maps namespace ids to namespace URIs. */ public static String getXPath(Element rootElement, Element targetElement, boolean includeElementIndex, Map<String, String> namespacesMap) { Stack<Element> elementPath = new Stack<Element>(); // since we need the mapping the other way round, we invert the map Map<String, String> namespaceUriToIdMap = new HashMap<String, String>(); for (Entry<String, String> entry : namespacesMap.entrySet()) { namespaceUriToIdMap.put(entry.getValue(), entry.getKey()); } // recursively find all ancestors of targetElement (up to, not including, rootElement) { Element currentElement = targetElement; while (currentElement != null && currentElement != rootElement) { elementPath.push(currentElement); Node parent = currentElement.getParentNode(); if (parent instanceof Element) { currentElement = (Element) currentElement.getParentNode(); } else { currentElement = null; } } } // construct XPath StringBuilder builder = new StringBuilder(); while (!elementPath.isEmpty()) { Element currentElement = elementPath.pop(); if (builder.length() > 0) { // don't include "/" at the beginning builder.append("/"); } if (namespacesMap != null) { String namespace = currentElement.getNamespaceURI(); if (namespace != null) { namespace = namespaceUriToIdMap.get(namespace); builder.append(namespace); builder.append(":"); } } builder.append(currentElement.getLocalName()); if (includeElementIndex) { int index = getElementIndex(currentElement); builder.append("["); builder.append(index); builder.append("]"); } } return builder.toString(); }
From source file:Main.java
/** * Returns a valid Java name from an XML Name. * * @param name/* w w w .ja va2s .co m*/ * @param isUpperCase * @return a valid Java name from an XML Name */ public static String getJavaNameFromXMLName(String name, boolean isUpperCase) { List<String> parsedName = parseName(name, '_'); StringBuilder result = new StringBuilder(64 * parsedName.size()); for (String nameComponent : parsedName) { if (nameComponent.length() > 0) { if (result.length() > 0 || isUpperCase) { result.append(Character.toUpperCase(nameComponent.charAt(0))); result.append(nameComponent.substring(1)); } else { result.append(nameComponent); } } } if (result.length() == 0) { return "_"; } if (Character.isJavaIdentifierStart(result.charAt(0))) { return isUpperCase ? result.toString() : decapitalizeName(result.toString()); } return "_" + result; }
From source file:Main.java
private static CharSequence consume(URLConnection connection, int maxChars) throws IOException { String encoding = getEncoding(connection); StringBuilder out = new StringBuilder(); Reader in = null;//w w w . ja v a2s. c o m try { in = new InputStreamReader(connection.getInputStream(), encoding); char[] buffer = new char[1024]; int charsRead; while (out.length() < maxChars && (charsRead = in.read(buffer)) > 0) { out.append(buffer, 0, charsRead); } } finally { if (in != null) { try { in.close(); } catch (IOException ioe) { // continue } catch (NullPointerException ioe) { // continue } } } return out; }