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:Main.java

public static String toString(String[] permission) {
    if (permission == null || permission.length <= 0) {
        return "";
    }//from w ww.ja  va2s. c o m

    StringBuilder sb = new StringBuilder();
    for (String p : permission) {
        sb.append(p.replaceFirst("android.permission.", ""));
        sb.append(",");
    }

    sb.deleteCharAt(sb.length() - 1);

    return sb.toString();
}

From source file:com.cyberway.issue.io.arc.ARC2WCDX.java

protected static void appendField(StringBuilder builder, Object obj) {
    if (builder.length() > 0) {
        // prepend with delimiter
        builder.append(' ');
    }//from   www . ja  va2  s  .  c o m
    if (obj instanceof Header) {
        obj = ((Header) obj).getValue().trim();
    }

    builder.append((obj == null || obj.toString().length() == 0) ? "-" : obj);
}

From source file:Main.java

/**
 * Returns MAC address of the given interface name.
 *
 * @param interfaceName eth0, wlan0 or NULL=use first interface
 * @return mac address or empty string/*from w  w w  .  j  a v  a2 s .  c o m*/
 */
public static String getMACAddress(String interfaceName) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            if (interfaceName != null) {
                if (!intf.getName().equalsIgnoreCase(interfaceName))
                    continue;
            }
            byte[] mac = intf.getHardwareAddress();
            if (mac == null)
                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);
            return buf.toString();
        }
    } catch (Exception ex) {
    } // for now eat exceptions
    return "";
    /*try {
    // this is so Linux hack
    return loadFileAsString("/sys/class/net/" +interfaceName + "/address").toUpperCase().trim();
    } catch (IOException ex) {
    return null;
    }*/
}

From source file:de.tudarmstadt.ukp.csniper.webapp.search.tgrep.PennTreeUtils.java

private static void toText(StringBuilder aBuffer, PennTreeNode aNode) {
    if (aNode.isTerminal()) {
        if (aBuffer.length() > 0) {
            aBuffer.append(" ");
        }/*from   w w w.j  ava2  s  .c o  m*/
        String label = aNode.getLabel();
        if ("-LRB-".equals(label)) {
            aBuffer.append("(");
        } else if ("-RRB-".equals(label)) {
            aBuffer.append(")");
        } else {
            aBuffer.append(label);
        }
    } else {
        for (PennTreeNode n : aNode.getChildren()) {
            toText(aBuffer, n);
        }
    }
}

From source file:Main.java

public static String getUserAgent() {
    StringBuilder model = new StringBuilder(android.os.Build.MODEL);
    if (model.toString().startsWith("\"")) {
        model.deleteCharAt(0);// ww w  .  j a v a  2  s .  c  om
    }
    if (model.toString().endsWith("\"")) {
        model.deleteCharAt(model.length() - 1);
    }
    return model.toString();
}

From source file:com.pkrete.xrd4j.rest.util.ClientUtil.java

/**
 * Builds the target URL based on the given based URL and parameters Map.
 * @param url base URL/*from  ww  w.  ja v a  2s . c  o m*/
 * @param params URL parameters
 * @return comlete URL containing the base URL with all the parameters
 * appended
 */
public static String buildTargetURL(String url, Map<String, String> params) {
    logger.debug("Target URL : \"{}\".", url);
    if (params == null || params.isEmpty()) {
        logger.debug("URL parameters list is null or empty. Nothing to do here. Return target URL.");
        return url;
    }
    if (params.containsKey("resourceId")) {
        logger.debug("Resource ID found from parameters map. Resource ID value : \"{}\".",
                params.get("resourceId"));
        if (!url.endsWith("/")) {
            url += "/";
        }
        url += params.get("resourceId");
        params.remove("resourceId");
        logger.debug("Resource ID added to URL : \"{}\".", url);
    }

    StringBuilder paramsString = new StringBuilder();
    for (String key : params.keySet()) {
        if (paramsString.length() > 0) {
            paramsString.append("&");
        }
        paramsString.append(key).append("=").append(params.get(key));
        logger.debug("Parameter : \"{}\"=\"{}\"", key, params.get(key));
    }

    if (!url.contains("?") && !params.isEmpty()) {
        url += "?";
    } else if (url.contains("?") && !params.isEmpty()) {
        if (!url.endsWith("?") && !url.endsWith("&")) {
            url += "&";
        }
    }
    url += paramsString.toString();
    logger.debug("Request parameters added to URL : \"{}\".", url);
    return url;
}

From source file:Main.java

public static String request(String url, Map<String, String> cookies, Map<String, String> parameters)
        throws Exception {
    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    connection.setRequestProperty("User-Agent",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36");
    if (cookies != null && !cookies.isEmpty()) {
        StringBuilder cookieHeader = new StringBuilder();
        for (String cookie : cookies.values()) {
            if (cookieHeader.length() > 0) {
                cookieHeader.append(";");
            }/*  w  w w.java 2  s .  com*/
            cookieHeader.append(cookie);
        }
        connection.setRequestProperty("Cookie", cookieHeader.toString());
    }
    connection.setDoInput(true);
    if (parameters != null && !parameters.isEmpty()) {
        connection.setDoOutput(true);
        OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
        osw.write(parametersToWWWFormURLEncoded(parameters));
        osw.flush();
        osw.close();
    }
    if (cookies != null) {
        for (Map.Entry<String, List<String>> headerEntry : connection.getHeaderFields().entrySet()) {
            if (headerEntry != null && headerEntry.getKey() != null
                    && headerEntry.getKey().equalsIgnoreCase("Set-Cookie")) {
                for (String header : headerEntry.getValue()) {
                    for (HttpCookie httpCookie : HttpCookie.parse(header)) {
                        cookies.put(httpCookie.getName(), httpCookie.toString());
                    }
                }
            }
        }
    }
    Reader r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    StringWriter w = new StringWriter();
    char[] buffer = new char[1024];
    int n = 0;
    while ((n = r.read(buffer)) != -1) {
        w.write(buffer, 0, n);
    }
    r.close();
    return w.toString();
}

From source file:Main.java

/**
 * Abbreviates a String by replacing a chunk in the middle by ellipses (...).
 * @param str the string to abbreviate.//from  w  ww .j  av  a 2  s  .  c  o  m
 * @param minLeft preserves a minimum number of characters at the start
 * @param maxLength the length of the abbreviated String
 * This will be ignored if less than minLeft + minRight + 3
 * @return the abbreviated string
 */
public static String abbreviate(String str, int minLeft, int maxLength) {
    if (str.length() <= maxLength) {
        return str;
    }
    int left = maxLength - ELIPSES.length();
    left = (left >= 0) ? left : 0;
    if (left > minLeft && minLeft >= 0) {
        left = minLeft;
    }

    int length = str.length();
    if (left > length) {
        left = length;
    }

    StringBuilder sb = new StringBuilder();
    sb.append(str.substring(0, left));
    sb.append(ELIPSES);

    int removeCount = length - maxLength + sb.length();
    if (removeCount > 0 && removeCount < length) {
        sb.append(str.substring(removeCount, length));
    }

    return sb.toString();

}

From source file:org.cloudfoundry.maven.common.CommonUtils.java

/**
 * Right-pad a String with a configurable padding character.
 *
 * @param string      The String to pad/*from  w w  w . j a  v a 2 s.co  m*/
 * @param size        Pad String by the number of characters.
 * @param paddingChar The character to pad the String with.
 * @return The padded String. If the provided String is null, an empty String is returned.
 */
public static String padRight(String string, int size, char paddingChar) {

    if (string == null) {
        return "";
    }

    StringBuilder padded = new StringBuilder(string);
    while (padded.length() < size) {
        padded.append(paddingChar);
    }
    return padded.toString();
}

From source file:com.zuoxiaolong.niubi.job.persistent.hibernate.HibernateNamingStrategy.java

private static String addUnderscores(String name) {
    StringBuilder buf = new StringBuilder(name.replace('.', '_'));
    for (int i = 1; i < buf.length() - 1; i++) {
        if (Character.isLowerCase(buf.charAt(i - 1)) && Character.isUpperCase(buf.charAt(i))
                && Character.isLowerCase(buf.charAt(i + 1))) {
            buf.insert(i++, '_');
        }/*from   w  w w  . j  a va  2s.c om*/
    }
    return buf.toString().toLowerCase(Locale.ROOT);
}