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 toSafeXMLString(String str) {
    StringBuffer safeXMLStr = new StringBuffer();

    for (int i = 0; (str != null) && (i < str.length()); i++) {
        char ch = str.charAt(i);

        if (ch == '<')
            safeXMLStr.append("&lt;");
        else if (ch == '>')
            safeXMLStr.append("&gt;");
        else if (ch == '&')
            safeXMLStr.append("&amp;");
        else if (ch == '"')
            safeXMLStr.append("&quot;");
        else {/*  w  w w.jav a2s  .c o  m*/
            safeXMLStr.append(ch);
        }
    }
    return safeXMLStr.toString();
}

From source file:Main.java

public static String inputStream2String(InputStream in) {
    StringBuffer out = new StringBuffer();
    byte[] b = new byte[4096];
    try {//from www . j  a v a 2  s  .c o m
        for (int n; (n = in.read(b)) != -1;) {
            out.append(new String(b, 0, n));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return out.toString();
}

From source file:Main.java

public static String getOnlyNumerics(String str) {
    if (str == null) {
        return null;
    }/*from w ww  . j  a v  a 2  s . co m*/

    StringBuffer strBuff = new StringBuffer();
    char c;
    for (int i = 0; i < str.length(); i++) {
        c = str.charAt(i);
        if (Character.isDigit(c)) {
            strBuff.append(c);
        }
    }
    return strBuff.toString();
}

From source file:Main.java

public static String bytesToPdu(byte[] bytes) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < bytes.length; i++) {
        sb.append(byteToPdu(bytes[i] & 0xFF));
    }//from w w w. j av  a2  s  .  c  o m
    return sb.toString();
}

From source file:Main.java

public static String removeBlanks(String content) {
    if (content == null) {
        return null;
    }//from w w w  . j a  v  a2 s. co 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[] split(String fileName) {
    StringBuffer nombre = new StringBuffer();
    StringBuffer extension = new StringBuffer();
    boolean ext = false;
    char[] cars = fileName.toCharArray();
    for (int i = 0; i < cars.length; i++) {
        if (cars[i] == '.') {
            if (ext) {
                nombre.append('.');
                nombre.append(extension.toString());
                extension = new StringBuffer();
            } else {
                ext = true;/*  w  ww. j  ava2 s  .c o m*/
            }
        } else {
            if (ext) {
                extension.append(cars[i]);
            } else {
                nombre.append(cars[i]);
            }
        }
    }
    String[] split = new String[2];
    split[0] = nombre.toString();
    split[1] = extension.toString();
    return split;
}

From source file:Main.java

public static String addAttribute(String p_name, String p_value) {
    StringBuffer l_buf = null;//from   w  w  w.  ja va2s .c  o m

    if (p_value != null) {
        l_buf = new StringBuffer();
        l_buf.append(" ").append(p_name).append("=\"");
        l_buf.append(p_value).append("\"");
        return l_buf.toString();
    }

    return "";
}

From source file:Main.java

public static String getFullName(String firstName, String lastName) {
    StringBuffer result = new StringBuffer();
    boolean flag = false;
    if (lastName != null && !lastName.isEmpty()) {
        result.append(lastName);//from w w  w  . j  av a  2  s .c  o m
        flag = true;
    }
    if (firstName != null && !firstName.isEmpty()) {
        if (flag) {
            result.append(", ");
        }
        result.append(firstName);
    }

    return result.toString();
}

From source file:Main.java

public static String getXMLString(String filePath) {
    StringBuffer sb = new StringBuffer();
    BufferedReader br = null;/*from  www.  j a va 2 s .  c  o m*/
    String line;
    try {
        br = new BufferedReader(new FileReader(filePath));
        while (true) {
            line = br.readLine();
            if (line == null) {
                break;
            }
            sb.append(line + "\n");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}

From source file:Main.java

public static String getNodeText(Node node) {
    if (node == null)
        return null;
    StringBuffer buff = new StringBuffer();
    for (int c = 0; c < node.getChildNodes().getLength(); c++) {
        Node cn = node.getChildNodes().item(c);
        if (cn.getNodeType() == Node.TEXT_NODE || cn.getNodeType() == Node.CDATA_SECTION_NODE) {
            buff.append(cn.getNodeValue());
        }//from ww  w. j  ava  2  s  . co m
    }
    return buff.toString().trim();

}