Example usage for java.lang StringBuffer StringBuffer

List of usage examples for java.lang StringBuffer StringBuffer

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public StringBuffer() 

Source Link

Document

Constructs a string buffer with no characters in it and an initial capacity of 16 characters.

Usage

From source file:Main.java

public static String htmlEncode(String s) {
    StringBuffer stringbuffer = new StringBuffer();
    int j = s.length();
    for (int i = 0; i < j; i++) {
        char c = s.charAt(i);
        switch (c) {
        case 60://from   ww  w . j  av a 2 s  .c  om
            stringbuffer.append("&lt;");
            break;
        case 62:
            stringbuffer.append("&gt;");
            break;
        case 38:
            stringbuffer.append("&amp;");
            break;
        case 34:
            stringbuffer.append("&quot;");
            break;
        case 169:
            stringbuffer.append("&copy;");
            break;
        case 174:
            stringbuffer.append("&reg;");
            break;
        case 165:
            stringbuffer.append("&yen;");
            break;
        case 8364:
            stringbuffer.append("&euro;");
            break;
        case 8482:
            stringbuffer.append("&#153;");
            break;
        case 13:
            if (i < j - 1 && s.charAt(i + 1) == 10) {
                stringbuffer.append("<br>");
                i++;
            }
            break;
        case 32:
            if (i < j - 1 && s.charAt(i + 1) == ' ') {
                stringbuffer.append(" &nbsp;");
                i++;
                break;
            }
        default:
            stringbuffer.append(c);
            break;
        }
    }
    return new String(stringbuffer.toString());
}

From source file:Main.java

private static StringBuffer getParamString(Map<String, String> params) {
    StringBuffer result = new StringBuffer();
    Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<String, String> param = iterator.next();
        String key = param.getKey();
        String value = param.getValue();
        result.append(key).append('=').append(value);
        if (iterator.hasNext()) {
            result.append('&');
        }//from  www  .  jav  a2s. c om
    }
    return result;
}

From source file:Main.java

/**
 * @param uid/*  w  w  w.  j a v  a  2s. c  om*/
 * @return
 */
public static String getUserUid(int uid) {
    String uidStr = String.valueOf(uid);
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < 6 - uidStr.length(); i++) {
        sb.append("0");
    }
    sb.append(uidStr);
    return sb.toString();
}

From source file:Main.java

private static StringBuffer getParamString(Map<String, Object> params) {
    StringBuffer result = new StringBuffer();
    Iterator<Map.Entry<String, Object>> iterator = params.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<String, Object> param = iterator.next();
        String key = param.getKey();
        Object value = param.getValue();
        result.append(key).append('=').append(value);
        if (iterator.hasNext()) {
            result.append('&');
        }/*  www.j ava 2 s .co m*/
    }
    return result;
}

From source file:Main.java

public static String fromat(Map<String, String> map, String relname, String crid) {
    StringBuffer xml = new StringBuffer();
    xml.append("<root>");
    for (Entry<String, String> en : map.entrySet()) {
        xml.append("<attr n='" + en.getKey() + "' d='text'><![CDATA[" + en.getValue() + "]]></attr>");
    }//from   w ww. j a va2 s. c om
    xml.append("<rel n='" + relname + "' d='from'><![CDATA[" + crid + "]]></rel>");
    xml.append("</root>");
    return xml.toString();

}

From source file:Main.java

@SuppressLint("DefaultLocale")
public static String byteToHexString(byte[] b) {
    StringBuffer hexString = new StringBuffer();
    for (int i = 0; i < b.length; i++) {
        String hex = Integer.toHexString(b[i] & 0xFF);
        if (hex.length() == 1) {
            hex = '0' + hex;
        }//  w w w  .j  a  v a 2s .  c  o  m
        hexString.append(hex.toUpperCase());
    }
    return hexString.toString();
}

From source file:Main.java

public static String printNode(Node node) {
    StringBuffer sBuffer = new StringBuffer();

    printNode(sBuffer, node);//from w w w. java2 s.c om

    return sBuffer.toString();
}

From source file:Main.java

public static String getTodayYYYYMMDD() {
    Calendar calendar = Calendar.getInstance();
    StringBuffer sbDate = new StringBuffer();
    int nYear, nMonth, nDay;
    nYear = calendar.get(Calendar.YEAR);
    nMonth = calendar.get(Calendar.MONTH) + 1;
    nDay = calendar.get(Calendar.DAY_OF_MONTH);
    sbDate.append(nYear);/* ww w. j  a  v  a2  s. co m*/
    if (nMonth < 10)
        sbDate.append("0");
    sbDate.append(nMonth);
    if (nDay < 10)
        sbDate.append("0");
    sbDate.append(nDay);

    return sbDate.toString();
}

From source file:Main.java

public static String convertToHex(byte[] data) {
    if (data == null || data.length == 0)
        return null;

    final StringBuffer buf = new StringBuffer();
    for (int i = 0; i < data.length; i++) {
        int halfbyte = (data[i] >>> 4) & 0x0F;
        int two_halfs = 0;
        do {//  ww w.  ja va  2  s  . c o m
            if ((0 <= halfbyte) && (halfbyte <= 9))
                buf.append((char) ('0' + halfbyte));
            else
                buf.append((char) ('a' + (halfbyte - 10)));
            halfbyte = data[i] & 0x0F;
        } while (two_halfs++ < 1);
    }
    return buf.toString();
}

From source file:Main.java

public static String collectionToString(Collection c) {
    StringBuffer buff = new StringBuffer();
    int i = 0;//from w  ww  .  ja v a 2 s  .  c om
    int size = c.size();
    for (Iterator iter = c.iterator(); iter.hasNext();) {
        Object object = iter.next();
        if (size > 1 && i == (size - 1)) {
            buff.append("and ");
        }
        buff.append(object);
        if (i < (size - 1)) {
            if (size > 2) {
                buff.append(", ");
            } else {
                buff.append(" ");
            }
        }
        i++;
    }
    return buff.toString();

}