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 join(List<String> paramList) {
    StringBuilder localStringBuilder = new StringBuilder(11 * paramList.size());
    Iterator localIterator = paramList.iterator();
    while (localIterator.hasNext()) {
        String str = (String) localIterator.next();
        if (localStringBuilder.length() != 0)
            localStringBuilder.append(",");
        localStringBuilder.append(str);/*  w ww  .ja v  a2 s  . c  o m*/
    }
    return localStringBuilder.toString();
}

From source file:com.ihelpoo.app.common.DeviceUtil.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 ww  .j av  a2 s  . c om*/
 */
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
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:Main.java

public static String getHexString(String s) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if (('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')) {
            sb.append(c);/*w  ww.j  a v a 2 s . c o  m*/
        }
    }
    if ((sb.length() % 2) != 0) {
        sb.deleteCharAt(sb.length());
    }
    return sb.toString();
}

From source file:de.micromata.genome.util.text.StringCommaList.java

/**
 * Encode basic array./*from  w w  w.  j  av a  2s  . c  o  m*/
 * 
 * @param <T> the generic type
 * @param parts the parts
 * @return the string
 */
public static <T> String encodeBasicArray(T[] parts) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < parts.length; ++i) {
        if (sb.length() > 0) {
            sb.append(",");
        }
        sb.append(parts[i].toString());
    }
    return sb.toString();
}

From source file:com.kevinshen.beyondupnp.util.Utils.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 a  2  s .  c om
 */
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 "";
}

From source file:com.cloudera.cdk.data.PartitionExpression.java

/**
 * Convert a PartitionStrategy into a serialized expression. This can be used
 * to set a PartitionStrategy in an Avro property if the PartitionStrategy is
 * passed as an object./*from  ww  w  .  ja  v a 2 s.  co m*/
 */
public static String toExpression(PartitionStrategy partitionStrategy) {
    List<FieldPartitioner> fieldPartitioners = partitionStrategy.getFieldPartitioners();
    if (fieldPartitioners.size() == 1) {
        return toExpression(fieldPartitioners.get(0));
    }
    StringBuilder sb = new StringBuilder();
    sb.append("[");
    for (FieldPartitioner fieldPartitioner : fieldPartitioners) {
        if (sb.length() > 1) {
            sb.append(", ");
        }
        sb.append(toExpression(fieldPartitioner));
    }
    sb.append("]");
    return sb.toString();
}

From source file:Main.java

public static String addPadding(String t, String s, int num) {
    StringBuilder retVal;

    if (null == s || 0 >= num) {
        throw new IllegalArgumentException(INVALID_ARGUMENT);
    }/*from w ww  .java 2s .co  m*/

    if (s.length() <= num) {
        return s.concat(t);
    }

    retVal = new StringBuilder(s);

    for (int i = retVal.length(); i > 0; i -= num) {
        retVal.insert(i, t);
    }
    return retVal.toString();
}

From source file:Main.java

public static String join(List<String> paramList) {
    StringBuilder localStringBuilder = new StringBuilder(11 * paramList.size());
    Iterator localIterator = paramList.iterator();
    while (localIterator.hasNext()) {
        String str = (String) localIterator.next();
        if (localStringBuilder.length() != 0) {
            localStringBuilder.append(",");
        }//from   www . j  a  v a  2 s  . c o  m
        localStringBuilder.append(str);
    }
    return localStringBuilder.toString();
}

From source file:com.jiubang.core.util.HttpUtils.java

/**
 * Send an HTTP(s) request with POST parameters.
 * //from w  w  w . j  a  v  a 2s .c  o  m
 * @param parameters
 * @param url
 * @throws UnsupportedEncodingException
 * @throws IOException
 * @throws KeyManagementException
 * @throws NoSuchAlgorithmException
 */
static void doPost(Map<?, ?> parameters, URL url)
        throws UnsupportedEncodingException, IOException, KeyManagementException, NoSuchAlgorithmException {

    URLConnection cnx = getConnection(url);

    // Construct data
    StringBuilder dataBfr = new StringBuilder();
    Iterator<?> iKeys = parameters.keySet().iterator();
    while (iKeys.hasNext()) {
        if (dataBfr.length() != 0) {
            dataBfr.append('&');
        }
        String key = (String) iKeys.next();
        dataBfr.append(URLEncoder.encode(key, "UTF-8")).append('=')
                .append(URLEncoder.encode((String) parameters.get(key), "UTF-8"));
    }
    // POST data
    cnx.setDoOutput(true);

    OutputStreamWriter wr = new OutputStreamWriter(cnx.getOutputStream());
    Loger.d(LOG_TAG, "Posting crash report data");
    wr.write(dataBfr.toString());
    wr.flush();
    wr.close();

    Loger.d(LOG_TAG, "Reading response");
    BufferedReader rd = new BufferedReader(new InputStreamReader(cnx.getInputStream()));

    String line;
    while ((line = rd.readLine()) != null) {
        Loger.d(LOG_TAG, line);
    }
    rd.close();
}

From source file:com.codeabovelab.dm.common.security.token.SignedTokenServiceBackend.java

static String pack(String... strs) {
    StringBuilder sb = new StringBuilder();
    for (String str : strs) {
        if (sb.length() != 0) {
            sb.append(',');
        }/*from   w ww . j  a va2 s. c o m*/
        sb.append(str);
    }
    return sb.toString();
}