Example usage for java.lang StringBuffer length

List of usage examples for java.lang StringBuffer length

Introduction

In this page you can find the example usage for java.lang StringBuffer length.

Prototype

@Override
    public synchronized int length() 

Source Link

Usage

From source file:Main.java

public static String trimTail(StringBuffer sb, char tail) {
    if (sb.length() > 0 && sb.charAt(sb.length() - 1) == tail)
        sb.deleteCharAt(sb.length() - 1);
    return sb.toString();
}

From source file:Main.java

/**
 *
 * @param len//from  w w w . j  av a2 s .  com
 * @return
 */
public static String getRandomHex(int len) {
    Random r = new Random();
    StringBuffer sb = new StringBuffer();
    while (sb.length() < len) {
        sb.append(Integer.toHexString(r.nextInt()));
    }
    return sb.toString().substring(0, len).toUpperCase();
}

From source file:Main.java

public static String filterInvalid(String xml) {
    StringBuffer sb = new StringBuffer(xml);
    for (int i = 0; i < sb.length(); i++) {
        int c = sb.charAt(i);
        if (c < 0x20 && c != 0x09/*\t*/ && c != 0x0A/*\n*/ && c != 0x0D/*\r*/) {
            sb.delete(i, i + 1);// ww  w .  j a va 2 s.c o  m
        }
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Replaces Windows 1252 characters with their Unicode equivalents.
 * @param buffer the buffer to be edited 
 *///from w w  w . jav a 2  s.c o m
public static void fixWin1252(StringBuffer buffer) {
    for (int i = 0; i < buffer.length(); i++) {
        char ch = buffer.charAt(i);
        if ('\u0080' <= ch && ch <= '\u009F')
            buffer.setCharAt(i, win1252_80_9f[ch - '\u0080']);
    }
}

From source file:Main.java

/**
 * Trims leading and trailing spaces from a string buffer
 * @param buffer the buffer to be edited 
 *//*from w  w w  . jav  a 2 s.  c om*/
public static void trim(StringBuffer buffer) {
    int i = 0;
    while (i < buffer.length() && isSpace(buffer.charAt(i)))
        i++;
    buffer.delete(0, i);
    int j = buffer.length() - 1;
    while (j >= 0 && isSpace(buffer.charAt(j)))
        j--;
    buffer.setLength(j + 1);
}

From source file:Main.java

public static String join(String[] arry, String with) {
    StringBuffer buf = new StringBuffer();
    for (String s : arry) {
        if (buf.length() > 0) {
            buf.append(with);/*from   w  w w .  ja v a2 s  .c o  m*/
        }
        buf.append(s);
    }
    return buf.toString();
}

From source file:Main.java

/**
 * Convert umlaute to entities//from  w  w  w . j a v a2s.  c  o  m
 */
public static String encode(String value) {
    StringBuffer buffer = new StringBuffer(value);
    for (int i = 0; i < buffer.length(); i++) {
        if (buffer.charAt(i) > 127) {
            buffer.replace(i, i + 1, "__" + ((int) buffer.charAt(i)) + ";");
        }
    }
    return buffer.toString();
}

From source file:Main.java

public static String removeBlanks(String content) {
    if (content == null) {
        return null;
    }/* w w w .  j a  v a  2s.  c o  m*/
    StringBuffer buff = new StringBuffer();
    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 join(CharSequence separator, Collection<CharSequence> values) {
    StringBuffer sb = new StringBuffer();
    for (CharSequence value : values) {
        if (sb.length() > 0) {
            sb.append(separator);//from w  ww  . ja va 2 s  .c o m
        }
        sb.append(value);
    }
    return sb.toString();
}

From source file:Main.java

private static SecretKeySpec createKey(String key) {
    byte[] data = null;
    if (key == null) {
        key = "";
    }/*from ww  w. j av a  2  s  . c  o  m*/
    StringBuffer sb = new StringBuffer(16);
    sb.append(key);
    while (sb.length() < 16) {
        sb.append("0");
    }
    if (sb.length() > 16) {
        sb.setLength(16);
    }
    try {
        data = sb.toString().getBytes("UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new SecretKeySpec(data, "AES");
}