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

/** Looks in findin for all occurrences of find and replaces them with replacewith 
 * @param findin The string to find occurrences in
 * @param find The string to find//from  ww w  . j  ava2 s .  com
 * @param replacewith The string to replace found occurrences with
 * @return A string with all occurrences of find replaced.
 */
public static String replace(String findin, String find, String replacewith) {

    StringBuffer sb = new StringBuffer(findin);
    int i = 0;
    try {
        while (i <= sb.length() - find.length()) {
            if (sb.substring(i, i + find.length()).equalsIgnoreCase(find)) {
                sb.replace(i, i + find.length(), replacewith);
            }
            i++;
        }
    } catch (StringIndexOutOfBoundsException e) {
        // We hit the end of the string - do nothing and carry on
    }

    return sb.toString();
}

From source file:fr.cls.atoll.motu.library.converter.jaxb.LocaleAdapter.java

/**
 * Trim all occurences of the supplied leading character from the given String.
 * //from w w  w. ja  v a 2s. co m
 * @param str the String to check
 * @param leadingCharacter the leading character to be trimmed
 * @return the trimmed String
 */
private static String trimLeadingCharacter(String str, char leadingCharacter) {
    if (StringUtils.isEmpty(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:ju.ehealthservice.graphs.RespRateGraphImage.java

public static boolean save(String fileName, String img) {
    try {//  w w  w  .  j  a v a  2 s  .c  om
        StringBuffer data = new StringBuffer(img);
        data = new StringBuffer(data.substring(23));
        int c = 0;
        for (int i = 0; i < data.length(); i++) {
            if (data.charAt(i) == ' ') {
                c++;
                data.setCharAt(i, '+');
            }
        }
        byte[] imageByteArray = Base64.decodeBase64(data.toString());
        String ID = fileName.substring(0, fileName.indexOf('_'));
        String path = Constants.GRAPH_REPOSITORY_PATH + ID + "\\RespRate\\";
        fileName = fileName + ".jpg";
        FileOutputStream imageOutFile = new FileOutputStream(path + fileName);
        imageOutFile.write(imageByteArray);
        imageOutFile.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

/**
 * Replaces successive XML space characters by a single space character (<tt>' '</tt>)
 * then removes leading and trailing space characters if any.
 * //w  w w  . ja  v  a 2s  .  c  o m
 * @param value string to be processed
 * @return processed string
 */
public static final String collapseWhiteSpace(CharSequence value) {
    StringBuffer buffer = new StringBuffer();
    compressWhiteSpace(value, buffer);

    int last = buffer.length() - 1;
    if (last >= 0) {
        if (buffer.charAt(last) == ' ') {
            buffer.deleteCharAt(last);
            --last;
        }

        if (last >= 0 && buffer.charAt(0) == ' ')
            buffer.deleteCharAt(0);
    }

    return buffer.toString();
}

From source file:ju.ehealthservice.graphs.ECGGraphImage.java

public static boolean save(String fileName, String img) {
    try {// ww w.j a v  a2s. c  o  m
        StringBuffer data = new StringBuffer(img);
        data = new StringBuffer(data.substring(23));
        int c = 0;
        for (int i = 0; i < data.length(); i++) {
            if (data.charAt(i) == ' ') {
                c++;
                data.setCharAt(i, '+');
            }
        }
        byte[] imageByteArray = Base64.decodeBase64(data.toString());
        String ID = fileName.substring(0, fileName.indexOf('_'));
        String path = Constants.GRAPH_REPOSITORY_PATH + ID + "\\ECG\\";
        fileName = fileName + ".jpg";
        FileOutputStream imageOutFile = new FileOutputStream(path + fileName);
        imageOutFile.write(imageByteArray);
        imageOutFile.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

public static String pathUp(Node node, int level) {
    StringBuffer buf = new StringBuffer();
    int current = level;
    while (node != null && current > 0) {
        if (node instanceof Element) {
            if (buf.length() > 0) {
                buf.insert(0, "/");
            }/*from  w w  w  .  ja  va  2s  .co m*/
            buf.insert(0, node.getNodeName());
            current = current - 1;
        }
        node = node.getParentNode();
    }
    return buf.toString();
}

From source file:Main.java

/**
 * Joins an array by the given separator.
 * // www .j av  a 2s. c om
 * @param array
 *            the array to join
 * @param separator
 *            the joining separator
 * @return joined string
 */
public static String join(Object[] array, String separator) {
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < array.length; i++) {
        Object item = array[i];
        if (item != null) {
            if (buf.length() > 0) {
                buf.append(separator);
            }
            buf.append(item);
        }
    }
    return buf.toString();
}

From source file:Main.java

/**
 * Forms an hex-encoded String of the specified byte array.
 *
 * @param byteArray The byte array to be hex-encoded.
 *
 * @return An hex-encoded String of the specified byte array.
 *//*ww w . j a va  2 s.c  om*/
public static String byteArrayToHexString(byte[] byteArray) {
    if (byteArray == null) {
        return "";
    }

    StringBuffer sb = new StringBuffer();

    for (byte b : byteArray) {
        sb.append(String.format("%02X ", b & 0xFF));
    }

    return sb.substring(0, sb.length() - 1).toString();
}

From source file:com.neophob.sematrix.cli.PixConClient.java

/**
 * // ww  w .j  a v a 2 s  . c o m
 * @param s
 * @param length
 * @return
 */
private static String pretifyString(String s, int length) {
    StringBuffer sb = new StringBuffer();

    sb.append(s);
    while (sb.length() < length) {
        sb.append(' ');
    }

    return sb.toString();
}

From source file:Main.java

/**
 * collapse multi-whitespace into one whitespace in the original text.
 * Support unicode white space with isWhitespace and isSpaceChar method in
 * {@link #Character}//  ww  w  .  ja  v a  2 s. co  m
 * 
 * @param oriText
 * @return
 */
public static String collapseWhiteSpace(StringBuffer oriText) {
    if (oriText == null) {
        return null;
    }

    int oriLen = oriText.length();
    StringBuffer newText = new StringBuffer(oriLen);
    int wsCount = 0;
    char lastWs = ' ';

    for (int i = 0; i <= oriLen; i++) {
        if (i == oriLen) {
            if (wsCount == 1) {
                newText.append(lastWs);
            }

            if (wsCount > 1) {
                newText.append(' ');
            }

            wsCount = 0;
            break;
        }

        char c = oriText.charAt(i);
        if (Character.isWhitespace(c) || Character.isSpaceChar(c)) {
            wsCount++;
            lastWs = c;
        } else {
            if (wsCount == 1) {
                newText.append(lastWs);
            }

            if (wsCount > 1) {
                newText.append(' ');
            }

            wsCount = 0;

            newText.append(c);
        }
    }

    String relt = newText.toString();
    return relt;
}