Example usage for java.lang StringBuilder length

List of usage examples for java.lang StringBuilder length

Introduction

In this page you can find the example usage for java.lang StringBuilder length.

Prototype

int length();

Source Link

Document

Returns the length of this character sequence.

Usage

From source file:io.wcm.sling.commons.util.Escape.java

/**
 * Creates a valid node name. Replaces all chars not in a-z, A-Z and 0-9 or '_' with '-' and converts all to lowercase.
 * @param value String to be labelized.//from   www.  j  a  v a  2  s  . c  o m
 * @return The labelized string.
 */
public static String validName(String value) {

    // convert to lowercase
    String text = value.toLowerCase();

    // replace some special chars first
    text = StringUtils.replace(text, "", "ae");
    text = StringUtils.replace(text, "", "oe");
    text = StringUtils.replace(text, "", "ue");
    text = StringUtils.replace(text, "", "ss");

    // replace all invalid chars
    StringBuilder sb = new StringBuilder(text);
    for (int i = 0; i < sb.length(); i++) {
        char ch = sb.charAt(i);
        if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')
                || (ch == '_'))) {
            ch = '-';
            sb.setCharAt(i, ch);
        }
    }
    return sb.toString();
}

From source file:Main.java

public static boolean getRandomPermutation(StringBuilder sb) {
    for (int i = 0; i != sb.length(); ++i) {
        if (!getNextPermutation(sb))
            return false;
    }/*from  www . jav a 2  s. c  o  m*/

    return true;
}

From source file:Main.java

public static String collectionAsString(Collection<?> collection) {
    if (collection == null)
        return "[null]";
    if (collection.size() == 0)
        return "[empty]";
    StringBuilder builder = new StringBuilder();
    for (Object obj : collection) {
        builder.append(obj).append(", ");
    }//  ww w  .ja  v a 2  s . c  o  m
    builder.deleteCharAt(builder.length() - 1);
    return builder.toString();
}

From source file:Main.java

public static String joinLookup(Iterable<?> elements, String separator, Map<?, ?> lookup) {
    if (elements == null)
        return "";
    StringBuilder buf = new StringBuilder();
    if (separator == null)
        separator = " ";

    for (Object o : elements) {
        if (buf.length() > 0)
            buf.append(separator);//w w  w  .  j  a v a2 s  . c om
        buf.append(lookup.get(o));
    }
    return buf.toString();
}

From source file:Main.java

public static <T> String join(T[] array, String join) {
    StringBuilder builder = new StringBuilder();
    for (T t : array) {
        builder.append(t);//  www .  ja  v  a2  s . co  m
        builder.append(join);
    }
    return builder.deleteCharAt(builder.length() - 1).toString();
}

From source file:StringHelper.java

/**
 * Right pad the given text parameter/*  w w  w  . j a  v  a  2 s . c om*/
 * 
 * <pre>
 * <b>Assertions:</b>
 *    assertNull(StringHelper.rightPad(null, 10, '*'));
 *    assertEquals("", StringHelper.rightPad("", 0, '*'));
 *    assertEquals("*", StringHelper.rightPad("", 1, '*'));
 *    assertEquals("*****", StringHelper.rightPad("", 5, '*'));
 *    assertEquals("Hello*****", StringHelper.rightPad("Hello", 10, '*'));
 *    assertEquals("*****", StringHelper.rightPad("*****", 0, '*'));
 *    assertEquals("*****", StringHelper.rightPad("*****", 1, '*'));
 *    assertEquals("*****", StringHelper.rightPad("*****", 3, '*'));
 * </pre>
 * 
 * @param text
 * @param size
 * @param padChar
 * @return Right pad of the given text parameter
 */
public static String rightPad(final String text, final int size, final char padChar) {
    if (text == null) {
        return null;
    }

    final StringBuilder paddedText = new StringBuilder(size);
    paddedText.append(text);

    while (paddedText.length() < size) {
        paddedText.append(padChar);
    }
    return paddedText.toString();
}

From source file:Main.java

public static String appendWithSeparator(final Collection<?> items, final String sep,
        final boolean separatorAtEnd) {
    StringBuilder sb = new StringBuilder();
    for (Object o : items) {
        sb.append(o.toString());//  ww w.  ja va2  s.c om
        sb.append(sep);
    }
    if (sb.length() > 0 && !separatorAtEnd) {
        sb.delete(sb.length() - sep.length(), sb.length());
    }
    return sb.toString();
}

From source file:Main.java

public static String deserialBundle(Bundle bundle) {
    if (bundle == null)
        return null;
    StringBuilder params = new StringBuilder();
    for (String key : bundle.keySet()) {
        params.append(key + "=" + bundle.get(key) + "&");
    }/*w  w w . j a v a 2  s. c  o  m*/

    return params.length() > 0 ? params.substring(0, params.length() - 1) : null;
}

From source file:org.acra.util.HttpUtils.java

/**
 * Send an HTTP(s) request with POST parameters.
 * /*from w ww.  jav a 2s  . co m*/
 * @param parameters
 * @param url
 * @throws ClientProtocolException
 * @throws IOException
 */
public static void doPost(Map<?, ?> parameters, URL url, String login, String password)
        throws ClientProtocolException, IOException {

    // Construct data
    final StringBuilder dataBfr = new StringBuilder();
    for (final Object key : parameters.keySet()) {
        if (dataBfr.length() != 0) {
            dataBfr.append('&');
        }
        Object value = parameters.get(key);
        if (value == null) {
            value = "";
        }
        dataBfr.append(URLEncoder.encode(key.toString(), "UTF-8")).append('=')
                .append(URLEncoder.encode(value.toString(), "UTF-8"));
    }

    final HttpRequest req = new HttpRequest(isNull(login) ? null : login, isNull(password) ? null : password);
    req.sendPost(url.toString(), dataBfr.toString());
}

From source file:com.dtolabs.rundeck.core.utils.OptsUtil.java

public static String join(Collection<String> args) {
    StringBuilder sb = new StringBuilder();
    for (String str : args) {
        if (sb.length() > 0) {
            sb.append(" ");
        }//  w w  w.  j  a va 2 s  . c o  m
        sb.append(escapeChars(str));
    }
    return sb.toString();
}