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:com.webarch.common.lang.StringSeriesTools.java

/**
 * @param num //from   w  ww. j  a v a 2s  . c om
 * @return
 * @description ?
 */
public static String getCnNumber(int num) {
    if (num < 0) {//??
        return null;
    }
    String strNum = new Integer(num).toString();
    // ?
    StringBuffer newStr = new StringBuffer("");
    for (int i = strNum.length() - 1; i >= 0; i--) {
        int index = Integer.parseInt(String.valueOf(strNum.charAt(strNum.length() - i - 1)));
        newStr.append(numStr[index]).append(unit[i]);
    }
    String result = newStr.toString().replaceAll("", "").
            replaceAll("?", "").
            replaceAll("", "").
            replaceFirst("??", "").
            replaceAll("+", "").replaceFirst("??", "??").trim();
    // ???
    if (result.endsWith("")) {
        result = result.substring(0, newStr.length() - 1);
    }
    return result;
}

From source file:org.sakaiproject.imagegallery.springutil.SqlScriptParser.java

/**
 * Parse the SQL script to produce an array of database statements.
 * @param sqlScriptReader A reader that reads the SQL script text.
 * @return An array of strings, each containing a single statement.
 * @throws RuntimeException-wrapped IOException if the script cannot be read.
 * @throws RuntimeException-wrapped ParseException if the script cannot be parsed.
 *//* ww  w. j  av  a  2s. c om*/
public static String[] parse(Reader sqlScriptReader) {
    char statementDelimiter = ';';
    List<String> statements = new ArrayList<String>();

    StringBuffer sql = new StringBuffer(1024);
    String line = "";
    BufferedReader in = new BufferedReader(sqlScriptReader);
    int lineNumber = 0;

    // Read each line and build up statements.
    try {
        while ((line = in.readLine()) != null) {
            lineNumber++;
            // Trim
            line = cleanLine(line);

            // Check for statement delimiter change.
            Character newDelimiter = parseForNewStatementDelimiter(line);
            if (newDelimiter != null) {
                statementDelimiter = newDelimiter.charValue();
                continue;
            }

            // Validate and strip comments
            parseLine(line, sql, lineNumber, statementDelimiter);
            if (sql.length() > 0) {
                if (sql.charAt(sql.length() - 1) == statementDelimiter) {
                    // This line terminates the statement.
                    // Lose the delimiter.
                    String statement = sql.toString().substring(0, sql.length() - 1).trim();
                    if (statement.length() > 0) {
                        statements.add(statement);
                        s_log.debug("Found statement: " + statement);
                    }
                    // Clear buffer for the next statement.
                    sql.replace(0, sql.length(), "");
                } else {
                    // This line does not terminate the statement. Add a
                    // space and go on to the next one.
                    sql.append(" ");
                }
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    // Catch any statements not followed by delimiter.
    String orphanStatement = sql.toString().trim();
    if (orphanStatement.length() > 0) {
        statements.add(orphanStatement);
        s_log.debug("Found statement: " + orphanStatement);
    }

    String[] result = new String[statements.size()];
    statements.toArray(result);
    return result;
}

From source file:ca.simplegames.micro.utils.StringUtils.java

/**
 * Trim leading whitespace from the given String.
 *
 * @param str the String to check/*from w w  w. j av a 2s  .c  o  m*/
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimLeadingWhitespace(String str) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuffer buf = new StringBuffer(str);
    while (buf.length() > 0 && Character.isWhitespace(buf.charAt(0))) {
        buf.deleteCharAt(0);
    }
    return buf.toString();
}

From source file:ca.simplegames.micro.utils.StringUtils.java

/**
 * Trim trailing whitespace from the given String.
 *
 * @param str the String to check//from w  ww  .  j av a 2 s.c om
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimTrailingWhitespace(String str) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuffer buf = new StringBuffer(str);
    while (buf.length() > 0 && Character.isWhitespace(buf.charAt(buf.length() - 1))) {
        buf.deleteCharAt(buf.length() - 1);
    }
    return buf.toString();
}

From source file:ca.simplegames.micro.utils.StringUtils.java

/**
 * Trim all occurences of the supplied leading character from the given String.
 *
 * @param str              the String to check
 * @param leadingCharacter the leading character to be trimmed
 * @return the trimmed String//from w w w . j  a v a 2s . c o  m
 */
public static String trimLeadingCharacter(String str, char leadingCharacter) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuffer buf = new StringBuffer(str);
    while (buf.length() > 0 && buf.charAt(0) == leadingCharacter) {
        buf.deleteCharAt(0);
    }
    return buf.toString();
}

From source file:ca.simplegames.micro.utils.StringUtils.java

/**
 * Trim all occurences of the supplied trailing character from the given String.
 *
 * @param str               the String to check
 * @param trailingCharacter the trailing character to be trimmed
 * @return the trimmed String/*  w  w w .  j ava2s .  c o m*/
 */
public static String trimTrailingCharacter(String str, char trailingCharacter) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuffer buf = new StringBuffer(str);
    while (buf.length() > 0 && buf.charAt(buf.length() - 1) == trailingCharacter) {
        buf.deleteCharAt(buf.length() - 1);
    }
    return buf.toString();
}

From source file:com.vmware.bdd.utils.CommonUtil.java

public static void prettyOutputStrings(List<Object> objs, String fileName, String delimeter) throws Exception {
    StringBuffer buff = new StringBuffer();
    if (isBlank(delimeter)) {
        delimeter = System.lineSeparator();
    }//from w ww  .  j  a  v  a 2 s . co  m

    for (Object obj : objs) {
        if (obj != null) {
            String str = obj.toString();
            if (!isBlank(str)) {
                buff.append(str).append(delimeter);
            }
        }
    }
    if (buff.length() > 0) {
        buff.delete(buff.length() - delimeter.length(), buff.length());
    }

    OutputStream out = null;
    BufferedWriter bw = null;
    try {
        if (!isBlank(fileName)) {
            out = new FileOutputStream(fileName);
        } else {
            out = System.out;
        }
        bw = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
        bw.write(buff.toString());
        if (!isBlank(fileName)) {
            // [Bug 1406542] always append a newline at the end of the file
            bw.newLine();
        }
        bw.flush();
    } finally {
        if (bw != null && out != null && !(out instanceof PrintStream)) {
            bw.close();
            out.close();
        }
    }
}

From source file:ca.simplegames.micro.utils.StringUtils.java

/**
 * Trim leading and trailing whitespace from the given String.
 *
 * @param str the String to check//from ww  w.  j a v a  2  s .c o m
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimWhitespace(String str) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuffer buf = new StringBuffer(str);
    while (buf.length() > 0 && Character.isWhitespace(buf.charAt(0))) {
        buf.deleteCharAt(0);
    }
    while (buf.length() > 0 && Character.isWhitespace(buf.charAt(buf.length() - 1))) {
        buf.deleteCharAt(buf.length() - 1);
    }
    return buf.toString();
}

From source file:com.fantasia.snakerflow.engine.SnakerHelper.java

public static String getPathJson(List<TransitionModel> tms) {
    StringBuffer buffer = new StringBuffer();
    buffer.append("paths:{");
    for (TransitionModel tm : tms) {
        buffer.append(tm.getName());/*from ww  w .  j av  a2 s.  co m*/
        buffer.append(":{from:'");
        buffer.append(tm.getSource().getName());
        buffer.append("',to:'");
        buffer.append(tm.getTarget().getName());
        buffer.append("', dots:[");
        if (StringUtils.isNotEmpty(tm.getG())) {
            String[] bendpoints = tm.getG().split(";");
            for (String bendpoint : bendpoints) {
                buffer.append("{");
                String[] xy = bendpoint.split(",");
                buffer.append("x:").append(getNumber(xy[0]));
                buffer.append(",y:").append(xy[1]);
                buffer.append("},");
            }
            buffer.deleteCharAt(buffer.length() - 1);
        }
        buffer.append("],text:{text:'");
        buffer.append(tm.getDisplayName());
        buffer.append("'},textPos:{");
        if (StringUtils.isNotEmpty(tm.getOffset())) {
            String[] values = tm.getOffset().split(",");
            buffer.append("x:").append(values[0]).append(",");
            buffer.append("y:").append(values[1]).append("");
        }
        buffer.append("}, props:{name:{value:'" + tm.getName() + "'},expr:{value:'" + tm.getExpr() + "'}}}");
        buffer.append(",");
    }
    buffer.deleteCharAt(buffer.length() - 1);
    buffer.append("},");
    return buffer.toString();
}

From source file:com.liferay.util.Http.java

public static String parameterMapToString(Map parameterMap, boolean addQuestion) {

    StringBuffer sb = new StringBuffer();

    if (parameterMap.size() > 0) {
        if (addQuestion) {
            sb.append(StringPool.QUESTION);
        }/*w  w  w .j  a v a 2 s.  c o  m*/

        Iterator itr = parameterMap.entrySet().iterator();

        while (itr.hasNext()) {
            Map.Entry entry = (Map.Entry) itr.next();

            String name = (String) entry.getKey();
            String[] values = (String[]) entry.getValue();

            for (int i = 0; i < values.length; i++) {
                sb.append(name);
                sb.append(StringPool.EQUAL);
                sb.append(Http.encodeURL(values[i]));
                sb.append(StringPool.AMPERSAND);
            }
        }

        sb.deleteCharAt(sb.length() - 1);
    }

    return sb.toString();
}