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 int2String(int i, int digits, boolean leadingZero) {
    StringBuilder s = new StringBuilder(digits);
    s.append(Integer.toString(i));
    while (s.length() < digits) {
        s.insert(0, (leadingZero ? "0" : " "));
    }//from ww  w. j  av  a  2  s .  c om
    return s.toString();
}

From source file:Main.java

/**
 * Trim all occurrences of the supplied trailing character from the given String.
 * @param str the String to check//  w w  w .  j a va 2  s. c o m
 * @param trailingCharacter the trailing character to be trimmed
 * @return the trimmed String
 */
public static String trimTrailingCharacter(String str, char trailingCharacter) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuilder sb = new StringBuilder(str);
    while (sb.length() > 0 && sb.charAt(sb.length() - 1) == trailingCharacter) {
        sb.deleteCharAt(sb.length() - 1);
    }
    return sb.toString();
}

From source file:Main.java

public static String toString(Iterable<?> iterable) {
    if (iterable == null)
        throw new IllegalArgumentException();

    StringBuilder sb = new StringBuilder();

    for (Object value : iterable) {
        if (sb.length() > 0) {
            sb.append(", ");
        }/*w  w w.ja  v  a 2 s  .c  o  m*/

        sb.append(value);
    }

    return sb.toString();
}

From source file:Main.java

public static String long2String(long l, int minDigits) {
    StringBuilder s = new StringBuilder(minDigits);
    s.append(Long.toString(l));/*  w  w  w.  j  av  a  2s  . co m*/
    while (s.length() < minDigits) {
        s.insert(0, "0");
    }
    return s.toString();
}

From source file:Main.java

public static String getNiceList(Collection<?> collection, String separator) {
    StringBuilder result = new StringBuilder();
    for (Object value : collection) {
        if (result.length() > 0) {
            result.append(separator);// w  w  w .j a v a  2  s .c  o m
        }
        result.append(value.toString());
    }
    return result.toString();
}

From source file:Main.java

public static List<String> generateWidth() {
    List<String> ids = new ArrayList<String>();
    Random mRandom = new Random();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 50; i++) {
        sb.delete(0, sb.length());
        int height = mRandom.nextInt(250) + 100;
        sb.append(height);//from   ww  w . jav  a2  s  . c  o m
        ids.add(sb.toString());
    }
    return ids;
}

From source file:Main.java

public static final String join(final Collection<?> list, final String inbetween) {
    final StringBuilder builder = new StringBuilder();
    for (final Object object : list) {
        if (builder.length() > 0) {
            builder.append(inbetween);//from  w  w w .j a va 2s.  c om
        }
        builder.append(object.toString());
    }
    return builder.toString();
}

From source file:Main.java

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

From source file:Main.java

public static String stringFromArray(String[] array, char delim) {
    StringBuilder builder = new StringBuilder();
    for (String string : array) {
        if (builder.length() > 0) {
            builder.append(delim);/*  w  ww.j a  va2s. c om*/
        }
        builder.append(string);
    }
    return builder.toString();
}

From source file:Main.java

public static SpannableStringBuilder getFrontTextStyle(String frontText, String afterText, int frontColor,
        int frontTextSize) {
    StringBuilder sb = new StringBuilder();
    sb.append("/").append(afterText);
    int len = sb.length();
    sb.insert(0, frontText);//  www  .  ja v a2s . c o  m
    SpannableStringBuilder style = new SpannableStringBuilder(sb);
    style.setSpan(new ForegroundColorSpan(frontColor), 0, sb.length() - len,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    style.setSpan(new AbsoluteSizeSpan(frontTextSize), 0, sb.length() - len,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return style;
}