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 asCsv(String[] arr) {
    StringBuffer tagcsv = new StringBuffer();
    for (String tag : arr) {
        if (tagcsv.length() > 0) {
            tagcsv.append(",");
        }/*from w  ww  .  java  2s . c  om*/
        tagcsv.append(tag);
    }
    return tagcsv.toString();
}

From source file:Main.java

/**
 * Return true if the string starting at offset in sb matches with xmlTag.
 * @param sb StringBuffer/*  w  ww.  jav a 2  s . co m*/
 * @param offset int
 * @param xmlTag String The XML tag name to check without '<' and '>'
 * @return
 */
private static boolean matchXMLTag(StringBuffer sb, int offset, String xmlTag) {
    if (offset >= sb.length()) {
        return false;
    }
    if (sb.charAt(offset) != '<') {
        return false;
    }
    int indexOfSpace = sb.indexOf(" ", offset);
    int indexOfGt = sb.indexOf(">", offset);
    int indexOfEndTag = Integer.MAX_VALUE;
    if (indexOfSpace >= 0) {
        indexOfEndTag = indexOfSpace;
    }
    if (indexOfGt >= 0 && indexOfGt < indexOfEndTag) {
        indexOfEndTag = indexOfGt;
    }
    if (indexOfEndTag == Integer.MAX_VALUE) {
        return false;
    }
    String potentialTag = sb.substring(offset + 1, indexOfEndTag);
    return potentialTag.equals(xmlTag);
}

From source file:Main.java

public static String toString(Enumeration/*<String>*/ enumer, String separator) {
    if (separator == null)
        separator = ","; //NOI18N
    StringBuffer buf = new StringBuffer();
    while (enumer.hasMoreElements()) {
        if (buf.length() != 0)
            buf.append(separator);/*from   ww w.  j  ava2  s. c om*/
        buf.append(enumer.nextElement());
    }
    return buf.toString();
}

From source file:Main.java

/**
 * Replaces HTML entities in a string buffer
 * @param buffer the string buffer/*from  ww  w .ja v  a 2s .  c o  m*/
 */
public static void replaceEntities(StringBuffer buffer) {
    int i = 0;
    while (i < buffer.length()) {
        if (buffer.charAt(i) == '&') {
            int j = i + 1;
            while (j < buffer.length() && buffer.charAt(j) != ';')
                j++;
            if (j < buffer.length()) {
                char[] chars = new char[j - i - 1];
                buffer.getChars(i + 1, j, chars, 0);
                Character repl = (Character) replacements.get(new String(chars));
                if (repl != null) {
                    buffer.delete(i, j);
                    buffer.setCharAt(i, repl.charValue());
                } else
                    i = j;
            } else
                i = j;
        }
        i++;
    }
}

From source file:Main.java

public static String getXmlEncoded(String text) {
    StringBuffer buf = new StringBuffer(text);
    for (int i = 0; i < buf.length(); ++i) {
        char c = buf.charAt(i);
        if (c == '&') {
            buf.replace(i, i + 1, "&amp;");
        } else if (c == '<') {
            buf.replace(i, i + 1, "&lt;");
        } else if (c == '>') {
            buf.replace(i, i + 1, "&gt;");
        } else if (c == '"') {
            buf.replace(i, i + 1, "&quot;");
        } else if (c == '\'') {
            buf.replace(i, i + 1, "&#39;");
        }//w  w  w  . j  av  a 2s. c  om
    }
    return buf.toString();
}

From source file:XMLCharacterRecognizer.java

/**
 * Tell if the string is whitespace./*from w  w w .j  ava  2s.  c  o  m*/
 *
 * @param buf StringBuffer to check as XML whitespace.
 * @return True if characters in buffer are XML whitespace, false otherwise
 */
public static boolean isWhiteSpace(StringBuffer buf) {

    int n = buf.length();

    for (int i = 0; i < n; i++) {
        if (!isWhiteSpace(buf.charAt(i)))
            return false;
    }

    return true;
}

From source file:Main.java

private static String getDumpStack(StackTraceElement[] se) {
    String CRLF = System.getProperty("line.separator");
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < se.length; i++) {
        if (sb.length() > 0) {
            sb.append(CRLF);//from w w  w . ja  v  a  2s .  c o  m
        }
        sb.append("\t" + se[i]);
    }
    return sb.toString();
}

From source file:Main.java

public static String getSringBeforeChar(String sourceStr, char ch) {
    StringBuffer sb = new StringBuffer(sourceStr);
    StringBuffer newSb = new StringBuffer();
    for (int i = 0; i < sb.length(); i++) {
        if (sb.charAt(i) == ch) {
            break;
        }//w  w  w.j  ava  2 s .  com
        newSb.append(sb.charAt(i));
    }

    return newSb.toString();

}

From source file:Main.java

/**
 * Convert the list of Integers into a comma separated
 * string so that it's easy to load back in.
 * @param list/*ww  w . j av  a  2  s.co m*/
 * @return
 */
public static String toString(List<?> list) {
    StringBuffer buf = new StringBuffer();
    for (Object o : list)
        buf.append(o + ",");
    buf.deleteCharAt(buf.length() - 1);
    return buf.toString();
}

From source file:Main.java

private static void appendTypeName(StringBuffer sb, Set typeNamesAppended, String name) {
    if (!typeNamesAppended.contains(name)) {
        if (sb.length() != 0)
            sb.append("+");
        sb.append(name);//from  ww  w .  j  a  v a 2s.  c  o  m
        typeNamesAppended.add(name);
    }
}