List of usage examples for java.lang StringBuilder length
int length();
From source file:Main.java
/** * Processes the cleartool output produced when invoking {@code launcher.run()} * to return it as a plain {@link String}. *//* ww w . ja va 2 s. c o m*/ public static String processCleartoolOuput(ByteArrayOutputStream baos) throws IOException { StringBuilder cleartoolOutput = new StringBuilder(); BufferedReader reader = new BufferedReader( new InputStreamReader(new ByteArrayInputStream(baos.toByteArray()))); String line = reader.readLine(); while (line != null) { if (cleartoolOutput.length() > 0) { cleartoolOutput.append('\n'); } cleartoolOutput.append(line); line = reader.readLine(); } reader.close(); return cleartoolOutput.toString(); }
From source file:Main.java
public static String replaceAndEndnote(String text, String holderString, String replacement, String noteTag, String noteSplit) {/*from w ww .ja va 2 s . c o m*/ StringBuilder note = new StringBuilder(); note.append(noteTag); String tmp = new String(text); int start = 0; while ((start = tmp.indexOf(holderString, start)) >= 0) { note.append(start + noteSplit); start += holderString.length(); } start = note.length() - noteSplit.length(); if (note.substring(start).equals(noteSplit)) note.delete(start, note.length()); return text.replace(holderString, replacement) + note; }
From source file:Main.java
public static String removeWhiteSpacesFromTag(String tag) { int index = tag.indexOf('/'); if (index == -1) { return tag.trim(); } else {/*from w ww . jav a 2s .c o m*/ final StringBuilder result = new StringBuilder(); int index0 = 0; while (true) { String subtag = (index == -1) ? tag.substring(index0).trim() : tag.substring(index0, index).trim(); if (subtag.length() > 0) { if (result.length() > 0) { result.append("/"); } result.append(subtag); } if (index == -1) { break; } index0 = index + 1; index = tag.indexOf('/', index0); } return result.toString(); } }
From source file:Main.java
public static String titleCase(String s) { StringBuilder rv = new StringBuilder(s.length()); StringTokenizer strtok = new StringTokenizer(s); while (strtok.hasMoreTokens()) { String word = strtok.nextToken(); String firstLetter = word.substring(0, 1); String restOfWord = word.substring(1); if (rv.length() > 0) rv.append(" "); rv.append(firstLetter.toUpperCase()); rv.append(restOfWord.toLowerCase()); }/*from w w w. jav a 2 s . c o m*/ return rv.toString(); }
From source file:hsyndicate.hadoop.utils.DFSNodeInfoUtils.java
public static String getDataNodesCommaSeparated(Configuration conf) throws IOException { StringBuilder sb = new StringBuilder(); DFSClient client = new DFSClient(NameNode.getAddress(conf), conf); DatanodeInfo[] datanodeReport = client.datanodeReport(HdfsConstants.DatanodeReportType.LIVE); for (DatanodeInfo nodeinfo : datanodeReport) { if (sb.length() != 0) { sb.append(","); }// w ww. j av a 2s . co m sb.append(nodeinfo.getHostName().trim()); } return sb.toString(); }
From source file:StringUtil.java
/** * Reverse the split operation./*from www . j av a 2s . c o m*/ * * @param parts * The parts to combine * @param index * the index to the fist part to use * @param length * the number of parts to use * @param splitter * The between-parts text */ public static String unsplit(String[] parts, int index, int length, String splitter) { if (parts == null) return null; if ((index < 0) || (index >= parts.length)) return null; if (index + length > parts.length) return null; StringBuilder buf = new StringBuilder(); for (int i = index; i < index + length; i++) { if (parts[i] != null) buf.append(parts[i]); buf.append(splitter); } // remove the trailing splitter buf.setLength(buf.length() - splitter.length()); return buf.toString(); }
From source file:Main.java
public static String post(String url, Map<String, String> params) { try {/*ww w .j a v a2 s . c o m*/ URL u = new URL(url); HttpURLConnection connection = (HttpURLConnection) u.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); PrintWriter pw = new PrintWriter(connection.getOutputStream()); StringBuilder sbParams = new StringBuilder(); if (params != null) { for (String key : params.keySet()) { sbParams.append(key + "=" + params.get(key) + "&"); } } if (sbParams.length() > 0) { String strParams = sbParams.substring(0, sbParams.length() - 1); Log.e("cat", "strParams:" + strParams); pw.write(strParams); pw.flush(); pw.close(); } BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuffer response = new StringBuffer(); String readLine = ""; while ((readLine = br.readLine()) != null) { response.append(readLine); } br.close(); return response.toString(); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
/** * Escapes a string builder so that it is valid XML. * /*w ww .j av a2s . co m*/ * @param sb * The string builder to escape. */ public static void escapeXML(StringBuilder sb) { // double quote -- quot // ampersand -- amp // less than -- lt // greater than -- gt // apostrophe -- apos for (int i = 0; i < sb.length();) { int codePoint = Character.codePointAt(sb, i); int length = Character.charCount(codePoint); if (codePoint == '<') { sb.replace(i, i + length, LT); i += LT.length(); } else if (codePoint == '>') { sb.replace(i, i + length, GT); i += GT.length(); } else if (codePoint == '\"') { sb.replace(i, i + length, QUOT); i += QUOT.length(); } else if (codePoint == '&') { sb.replace(i, i + length, AMP); i += AMP.length(); } else if (codePoint == '\'') { sb.replace(i, i + length, APOS); i += APOS.length(); } else { i += length; } } }
From source file:edu.msu.nscl.olog.Logs.java
/** * Creates a compact string representation for the log. * * @param data Log to create the string representation for * @return string representation// ww w . j a v a 2 s .c o m */ public static String toLogger(Logs data) { if (data.getLogs().size() == 0) { return "[None]"; } else { StringBuilder s = new StringBuilder(); s.append("["); for (Log c : data.getLogs()) { s.append(Log.toLogger(c) + ","); } s.delete(s.length() - 1, s.length()); s.append("]"); return s.toString(); } }
From source file:Main.java
/** * Returns the ASCII characters up to but not including the next "\r\n", or * "\n"./*from w ww. jav a 2 s.com*/ * * @throws java.io.EOFException if the stream is exhausted before the next newline * character. */ public static String readAsciiLine(InputStream in) throws IOException { StringBuilder result = new StringBuilder(80); while (true) { int c = in.read(); if (c == -1) { throw new EOFException(); } else if (c == '\n') { break; } result.append((char) c); } int length = result.length(); if (length > 0 && result.charAt(length - 1) == '\r') { result.setLength(length - 1); } return result.toString(); }