List of usage examples for java.lang StringBuilder length
int length();
From source file:Main.java
/** * Trim all occurrences of the supplied leading character from the given String. * @param str the String to check//from ww w. j a v a 2 s. co m * @param leadingCharacter the leading character to be trimmed * @return the trimmed String */ public static String trimLeadingCharacter(String str, char leadingCharacter) { if (!hasLength(str)) { return str; } StringBuilder sb = new StringBuilder(str); while (sb.length() > 0 && sb.charAt(0) == leadingCharacter) { sb.deleteCharAt(0); } return sb.toString(); }
From source file:com.fluidinfo.utils.StringUtil.java
/** * Joins an array of strings with the specified delimiter * @param s The strings to join//from w ww. ja v a 2 s .co m * @param delim The delimiter * @return The joined strings */ public static String join(String[] s, String delim) { if (s == null || s.length == 0) return ""; StringBuilder sb = new StringBuilder(); for (int i = 0; i < s.length; i++) { sb.append(s[i]).append(delim); } return sb.deleteCharAt(sb.length() - 1).toString(); }
From source file:Main.java
public static String join(int[] paramArrayOfInt) { StringBuilder localStringBuilder = new StringBuilder(11 * paramArrayOfInt.length); int i = paramArrayOfInt.length; for (int j = 0; j < i; j++) { int k = paramArrayOfInt[j]; if (localStringBuilder.length() != 0) { localStringBuilder.append(","); }/*from ww w .j a va2 s. c o m*/ localStringBuilder.append(k); } return localStringBuilder.toString(); }
From source file:com.epam.cme.storefront.util.MetaSanitizerUtil.java
/** * Takes a List of KeywordModels and returns a comma separated list of keywords as String. * /*from w w w .j av a2 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 sb = new StringBuilder(); for (final String kw : keywordSet) { sb.append(kw).append(','); } if (sb.length() > 0) { // Remove last comma return sb.substring(0, sb.length() - 1); } } return ""; }
From source file:Main.java
/** * Reads all text of the XML tag and returns it as a String. * Assumes that a '<' character has already been read. * * @param r The reader to read from// w w w. ja v a 2 s . com * @return The String representing the tag, or null if one couldn't be read * (i.e., EOF). The returned item is a complete tag including angle * brackets, such as <code><TXT></code> */ public static String readTag(Reader r) throws IOException { if (!r.ready()) { return null; } StringBuilder b = new StringBuilder("<"); int c = r.read(); while (c >= 0) { b.append((char) c); if (c == '>') { break; } c = r.read(); } if (b.length() == 1) { return null; } return b.toString(); }
From source file:com.linkedin.urls.PathNormalizer.java
License:asdf
/** * 1. Replaces "/./" with "/" recursively. * 2. "/blah/asdf/.." -> "/blah"//from w w w. ja v a2s . com * 3. "/blah/blah2/blah3/../../blah4" -> "/blah/blah4" * 4. "//" -> "/" * 5. Adds a slash at the end if there isn't one */ private static String sanitizeDotsAndSlashes(String path) { StringBuilder stringBuilder = new StringBuilder(path); Stack<Integer> slashIndexStack = new Stack<Integer>(); int index = 0; while (index < stringBuilder.length() - 1) { if (stringBuilder.charAt(index) == '/') { slashIndexStack.add(index); if (stringBuilder.charAt(index + 1) == '.') { if (index < stringBuilder.length() - 2 && stringBuilder.charAt(index + 2) == '.') { //If it looks like "/../" or ends with "/.." if (index < stringBuilder.length() - 3 && stringBuilder.charAt(index + 3) == '/' || index == stringBuilder.length() - 3) { boolean endOfPath = index == stringBuilder.length() - 3; slashIndexStack.pop(); int endIndex = index + 3; // backtrack so we can detect if this / is part of another replacement index = slashIndexStack.empty() ? -1 : slashIndexStack.pop() - 1; int startIndex = endOfPath ? index + 1 : index; stringBuilder.delete(startIndex + 1, endIndex); } } else if (index < stringBuilder.length() - 2 && stringBuilder.charAt(index + 2) == '/' || index == stringBuilder.length() - 2) { boolean endOfPath = index == stringBuilder.length() - 2; slashIndexStack.pop(); int startIndex = endOfPath ? index + 1 : index; stringBuilder.delete(startIndex, index + 2); // "/./" -> "/" index--; // backtrack so we can detect if this / is part of another replacement } } else if (stringBuilder.charAt(index + 1) == '/') { slashIndexStack.pop(); stringBuilder.deleteCharAt(index); index--; } } index++; } if (stringBuilder.length() == 0) { stringBuilder.append("/"); //Every path has at least a slash } return stringBuilder.toString(); }
From source file:net.GoTicketing.GetNecessaryCookie.java
/** * CookieMap ? http request //from w w w . j a v a 2 s.c o m * @param CookieMap Map ? cookie * @return http request */ public static String CookieMapToString(Map<String, String> CookieMap) { StringBuilder CookieBuilder = new StringBuilder(); CookieMap.forEach((key, value) -> { CookieBuilder.append("; ").append(key).append("=").append(value); }); if (CookieBuilder.length() > 2) return CookieBuilder.substring(2); else return CookieBuilder.toString(); }
From source file:Main.java
public static String join(long[] paramArrayOfLong) { StringBuilder localStringBuilder = new StringBuilder(11 * paramArrayOfLong.length); int i = paramArrayOfLong.length; for (int j = 0; j < i; j++) { long l = paramArrayOfLong[j]; if (localStringBuilder.length() != 0) { localStringBuilder.append(","); }//www .j av a2 s . c om localStringBuilder.append(l); } return localStringBuilder.toString(); }
From source file:net.certiv.antlr.project.util.Strings.java
/** * Convert a list of strings to a standard csv representation *//* w ww. ja v a 2 s.c om*/ public static String toCsv(List<String> strs) { StringBuilder sb = new StringBuilder(); for (String s : strs) { sb.append(s + ", "); } if (sb.length() > 1) sb.setLength(sb.length() - 2); return sb.toString(); }
From source file:Main.java
/** * Joins strings with the separator./*from w ww.jav a 2 s .c o m*/ * * @param collection * the collection * @param separator * the separator * @return a string joined with the separator */ public static String join(Collection<String> collection, String separator) { if (collection == null) { throw new NullPointerException("The collection parameter is null."); } if (separator == null) { throw new NullPointerException("The separator parameter is null."); } StringBuilder buf = new StringBuilder(); for (String s : collection) { buf.append("\""); buf.append(s); buf.append("\""); buf.append(separator); } if (buf.length() > 0) { buf.setLength(buf.length() - separator.length()); } return buf.toString(); }