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

/**
 * Limit the string to a certain number of characters, adding "..." if it was truncated
 * /*from   w  ww .j a va2 s.c  o m*/
 * @param value
 *        The string to limit.
 * @param length
 *        the length to limit to (as an int).
 * @return The limited string.
 */
public static String limit(String value, int length) {
    StringBuilder buf = new StringBuilder(value);
    if (buf.length() > length) {
        buf.setLength(length);
        buf.append("...");
    }

    return buf.toString();
}

From source file:Main.java

public static String StringArrayToString(List<String> array) {
    StringBuilder sb = new StringBuilder();
    for (String n : array) {
        if (sb.length() > 0)
            sb.append(',');
        sb.append("").append(n).append("");
    }// w  ww.j  a  v a  2 s  .  c  o m
    return sb.toString();
}

From source file:Main.java

/**
 * Creates a NMEA checksum for a sentence.
 * /*from w  w  w  .  ja  va2s  .c o  m*/
 * The checksum is calculated by XOR every char value, between '$' and
 * '*'(end), with the current sum.
 * 
 * @param sbString
 *            String to calculate the checksum.
 * @return The checksum.
 */
public static int getNMEAChecksum(final StringBuilder sbString) {
    int checksum = 0;

    for (int i = 0; i < sbString.length(); i++) {
        if (sbString.charAt(i) != '*' && sbString.charAt(i) != '$')
            checksum ^= sbString.charAt(i);
    }
    return checksum;
}

From source file:Main.java

private static SecretKeySpec generateAESKey(String password) {
    byte[] data = null;
    StringBuilder sb = new StringBuilder();
    sb.append(password);//from   w ww.  jav  a 2s.  com
    while (sb.length() < 16)
        sb.append("0");
    if (sb.length() > 16)
        sb.setLength(16);
    try {
        data = sb.toString().getBytes("UTF-8");
        return new SecretKeySpec(data, "AES");
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String join(final Collection<?> arr, final String sep) {
    final StringBuilder s = new StringBuilder();
    for (final Object obj : arr) {
        if (s.length() > 0)
            s.append(sep);//from  w  ww . ja  v  a 2  s.  c  om
        s.append(obj.toString());
    }
    return s.toString();
}

From source file:Main.java

public static String removeBlanks(String content) {
    if (content == null) {
        return null;
    }//from ww  w.j  ava  2 s  .  co  m
    StringBuilder buff = new StringBuilder();
    buff.append(content);
    for (int i = buff.length() - 1; i >= 0; i--) {
        if (' ' == buff.charAt(i) || ('\n' == buff.charAt(i)) || ('\t' == buff.charAt(i))
                || ('\r' == buff.charAt(i))) {
            buff.deleteCharAt(i);
        }
    }
    return buff.toString();
}

From source file:Main.java

public static String toUserEnteredString(List<String> term) {
    final StringBuilder builder = new StringBuilder();
    for (String part : term) {
        if (builder.length() != 0) {
            builder.append(' ');
        }/*from  w w w.j a  va 2 s  .  com*/
        builder.append(part);
    }
    return builder.toString();
}

From source file:Main.java

public static String genReqSerialNum() {
    if (reqSerialNum.get() == 99999999) {
        reqSerialNum.set(0);/*from w  ww.  j  av a2 s .co  m*/
    }
    int newValue = reqSerialNum.incrementAndGet();

    StringBuilder result = new StringBuilder(Integer.toString(newValue));
    int len = result.length();
    for (int i = len; i <= 8; i++) {
        result.insert(0, "0");
    }
    return result.toString();
}

From source file:Main.java

public static String abbreviate(String text) {
    String[] words = text.split("\\s");

    StringBuilder builder = new StringBuilder();
    int i = 0;/*from   w w  w .  j  a va  2  s  .c om*/
    while (i < words.length && builder.length() < 2) {
        String word = words[i].trim();
        if (word.length() > 0)
            builder.append(Character.toUpperCase(word.charAt(0)));
        i++;
    }

    return builder.toString();
}

From source file:Main.java

/**
 * Trim trailing whitespace from the given String.
 * @param str the String to check/*from w  ww . j  a  v a  2  s.c  o  m*/
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimTrailingWhitespace(String str) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuilder sb = new StringBuilder(str);
    while (sb.length() > 0 && Character.isWhitespace(sb.charAt(sb.length() - 1))) {
        sb.deleteCharAt(sb.length() - 1);
    }
    return sb.toString();
}