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

/**
 * Trim <i>all</i> whitespace from the given String:
 * leading, trailing, and in between characters.
 * @param str the String to check//from   w w  w.  j a va2  s.  co  m
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimAllWhitespace(String str) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuilder sb = new StringBuilder(str);
    int index = 0;
    while (sb.length() > index) {
        if (Character.isWhitespace(sb.charAt(index))) {
            sb.deleteCharAt(index);
        } else {
            index++;
        }
    }
    return sb.toString();
}

From source file:StringHelper.java

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

    final StringBuilder paddedText = new StringBuilder(size);
    while (paddedText.length() < size - text.length()) {
        paddedText.append(padChar);
    }

    paddedText.append(text);

    return paddedText.toString();
}

From source file:Main.java

public static String joins(String[] arrays, String joinStr) {
    if (arrays == null) {
        return "";
    }/*ww  w .j  a v  a  2  s. c om*/
    joinStr = StringUtils.hasText(joinStr) ? joinStr : ",";
    StringBuilder rs = new StringBuilder();
    for (String e : arrays) {
        rs.append(joinStr).append(e);
    }
    return rs.length() > 0 ? rs.substring(1) : "";
}

From source file:Main.java

/**
 * Format size in string form/*from  ww w  . ja va  2  s  .c  o  m*/
 *
 * @param size Size in bytes
 * @return Size in stinrg format with suffix
 */
public static String formatSize(int size) {
    String suffix = null;

    if (size >= 1024) {
        suffix = "KB";
        size /= 1024;
        if (size >= 1024) {
            suffix = "MB";
            size /= 1024;
        }
    }

    StringBuilder resultBuffer = new StringBuilder(Long.toString(size));

    int commaOffset = resultBuffer.length() - 3;
    while (commaOffset > 0) {
        resultBuffer.insert(commaOffset, ',');
        commaOffset -= 3;
    }

    if (suffix != null)
        resultBuffer.append(suffix);
    return resultBuffer.toString();
}

From source file:Main.java

public static String showByteArray(byte[] data) {
    if (null == data) {
        return null;
    }/*  w  ww. j ava2s.  co m*/
    StringBuilder sb = new StringBuilder("{");
    for (byte b : data) {
        sb.append(b).append(",");
    }
    sb.deleteCharAt(sb.length() - 1);
    sb.append("}");
    return sb.toString();
}

From source file:Main.java

public static String join(final long[] array, final char token) {
    final StringBuilder builder = new StringBuilder();
    for (final long item : array) {
        if (0 != builder.length()) {
            builder.append(token);/*from   w ww . j  a v  a2 s  .  c o m*/
        }
        builder.append(item);
    }
    return builder.toString();
}

From source file:Main.java

public static File convertFormat(File file, String newFormat) {
    StringBuilder tmpValue = new StringBuilder(file.getAbsolutePath());
    int index = tmpValue.lastIndexOf(".") + 1;
    tmpValue.delete(index, tmpValue.length());
    tmpValue.append(newFormat);/*from  www .  j a v  a2s.c om*/
    File result = new File(tmpValue.toString());

    return result;
}

From source file:Main.java

public static String join(final String[] array, final char token) {
    final StringBuilder builder = new StringBuilder();
    for (final String str : array) {
        if (0 != builder.length()) {
            builder.append(token);//from   ww w. ja  va  2  s .  co  m
        }
        builder.append(str);
    }
    return builder.toString();
}

From source file:cz.lbenda.coursing.user.UserRole.java

public static String toString(UserRole[] roles) {
    StringBuilder result = new StringBuilder();
    for (UserRole role : roles) {
        if (result.length() > 0) {
            result.append(",");
        }/*  w w  w .j av  a2  s.  c o m*/
        result.append(role.name());
    }
    return result.toString();
}

From source file:Main.java

public static String bytesToAddress(byte[] bytes) {
    StringBuilder sb = new StringBuilder();
    for (byte b : bytes) {
        sb.append(String.format("%02X:", b));
    }/*  w  w w . ja v a2 s.  co  m*/
    sb.deleteCharAt(sb.length() - 1); // remove last semicolon
    return sb.toString();
}