Example usage for java.lang StringBuffer setCharAt

List of usage examples for java.lang StringBuffer setCharAt

Introduction

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

Prototype

@Override
public synchronized void setCharAt(int index, char ch) 

Source Link

Usage

From source file:org.slage.framework.Tools.java

/**
 * Get a '?' delimited relative file path by starting with a localized
 * absolute path. This function will trim off the path to the working
 * directory and then replace file separators with ? characters (which can
 * then be read back into cross platform paths using GetQMarkDelimitedPath().
 * /*w  ww  . j  a  v  a  2s .  c  o  m*/
 * @param strPath an absolute file path (including file name if appropriate)
 * @return the '?' delimted relative path
 */
public static String BuildQMarkDelimitedPath(String strPath) {

    if (strPath == null) {
        return null;
    }

    String currentDirectory = System.getProperty("user.dir");
    if (strPath.startsWith(currentDirectory)) {
        strPath = strPath.substring(currentDirectory.length());
    }

    if (strPath.startsWith(GetFileSeparator())) {
        strPath = strPath.substring(GetFileSeparator().length());
    }

    StringBuffer sb = new StringBuffer(strPath);

    // For some reason, replaceAll crashes...
    for (int i = 0; i < strPath.length(); i++) {
        if (sb.charAt(i) == GetFileSeparator().charAt(0)) {
            sb.setCharAt(i, '?');
        }
    }
    return sb.toString();
}

From source file:gov.jgi.meta.MetaUtils.java

public static String applychangevectortovalues(String start, int[] v, int[] vv) {
    StringBuffer sb = new StringBuffer(start);
    char[] bases = { 'a', 't', 'g', 'c' };
    for (int i = 0; i < v.length; i++) {
        sb.setCharAt(v[i], bases[vv[i]]);
    }/*from  ww  w  .  j  av a 2 s .com*/
    return sb.toString();
}

From source file:Main.java

/**
/* FindAndReplace - finds and replaces in StringBuffer theSBuffer,
/*   starts at fromIndex, returns index of find.
 * /*from   w ww.  j a  v a2  s. co m*/
 * @param findString, replaceString, sBuffer, index
 * @return int
 * 
 */
public static int FindAndReplace(String find, String replace, StringBuffer theSBuffer, int fromIndex) {

    String interString;
    int theIndex, i, j;

    if (find == null)
        return -1;
    if (replace == null)
        return -1;
    if (theSBuffer == null)
        return -1;
    int theSBufferLength = theSBuffer.length();
    int findLength = find.length();
    if (theSBufferLength == 0)
        return -1;
    if (findLength == 0)
        return -1;
    if (theSBufferLength < findLength)
        return -1;
    if ((fromIndex < 0) || (fromIndex > theSBufferLength))
        return -1;

    interString = theSBuffer.toString();
    theIndex = interString.indexOf(find, fromIndex);
    if (theIndex == -1)
        return -1;

    //// on 9210 the following code ...
    for (i = theIndex; i < theSBufferLength - findLength; i++) {
        theSBuffer.setCharAt(i, theSBuffer.charAt(i + findLength));
    }
    for (j = theSBufferLength - 1; j >= (theSBufferLength - findLength); j--) {
        theSBuffer.setCharAt(j, (char) (0));
    }
    int newLength = theSBufferLength - findLength;
    theSBuffer.setLength(newLength);
    theSBuffer.insert(theIndex, replace);
    return theIndex;
}

From source file:org.wso2.andes.management.ui.views.ViewUtility.java

/**
 * Converts the input string to displayable format by converting some character case or inserting space
 * @param input/* w ww . j av  a 2  s . c o m*/
 * @return formatted string
 */
public static String getDisplayText(String input) {
    StringBuffer result = new StringBuffer(input);
    if (Character.isLowerCase(result.charAt(0))) {
        result.setCharAt(0, Character.toUpperCase(result.charAt(0)));
    }
    for (int i = 1; i < input.length(); i++) {
        if (Character.isUpperCase(result.charAt(i)) && !Character.isWhitespace(result.charAt(i - 1))
                && Character.isLowerCase(result.charAt(i - 1))) {
            result.insert(i, " ");
            i++;
        } else if (Character.isLowerCase(result.charAt(i)) && Character.isWhitespace(result.charAt(i - 1))) {
            result.setCharAt(i, Character.toUpperCase(result.charAt(i)));
        }

    }

    return result.toString();
}

From source file:org.yestech.lib.util.DecoderUtil.java

private static void uriDecode(final StringBuffer buffer, final int offset, final int length) {
    int index = offset;
    int count = length;
    for (; count > 0; count--, index++) {
        final char ch = buffer.charAt(index);
        if (ch != '%') {
            continue;
        }/*from  ww  w  .  j  a v a  2 s. c o  m*/
        if (count < 3) {
            throw new RuntimeException(
                    "invalid-escape-sequence.error: " + buffer.substring(index, index + count));
        }

        // Decode
        int dig1 = Character.digit(buffer.charAt(index + 1), 16);
        int dig2 = Character.digit(buffer.charAt(index + 2), 16);
        if (dig1 == -1 || dig2 == -1) {
            throw new RuntimeException("invalid-escape-sequence.error " + buffer.substring(index, index + 3));
        }
        char value = (char) (dig1 << 4 | dig2);

        // Replace
        buffer.setCharAt(index, value);
        buffer.delete(index + 1, index + 3);
        count -= 2;
    }
}

From source file:gov.jgi.meta.MetaUtils.java

/**
 * generate a set (unique) of all sequences ({ATGC} only) from start sequence
 * to distance steps (Hamming distance)//from   ww  w .j a v  a  2s .  co  m
 *
 * @param start the start sequence
 * @param distance the number of steps to travel
 * @return s set containing all neighbors including itself.
 */
public static Set<String> generateAllNeighbors(StringBuffer start, int distance) {
    char[] bases = { 'a', 't', 'g', 'c' };
    Set<String> s = new HashSet<String>();

    if (distance == 0) {
        s.add(start.toString());
        return (s);
    }

    for (int i = 0; i < start.length(); i++) {
        char old = start.charAt(i);
        for (char basePair : bases) {
            start.setCharAt(i, basePair);
            s.addAll(generateAllNeighbors(start, distance - 1));
        }
        start.setCharAt(i, old);
    }

    return (s);
}

From source file:org.exoplatform.wcm.webui.formgenerator.UIFormGeneratorTabPane.java

/**
 * Clean string.//from w  ww  .j a  v  a 2  s.com
 *
 * @param str the str
 *
 * @return the string
 */
private static String cleanString(String str) {
    Transliterator accentsconverter = Transliterator
            .getInstance("Latin; NFD; [:Nonspacing Mark:] Remove; NFC;");
    str = accentsconverter.transliterate(str);
    //the character ? seems to not be changed to d by the transliterate function
    StringBuffer cleanedStr = new StringBuffer(str.trim());
    // delete special character
    for (int i = 0; i < cleanedStr.length(); i++) {
        char c = cleanedStr.charAt(i);
        if (c == ' ') {
            if (i > 0 && cleanedStr.charAt(i - 1) == '-') {
                cleanedStr.deleteCharAt(i--);
            } else {
                c = '_';
                cleanedStr.setCharAt(i, c);
            }
            continue;
        }
        if (i > 0 && !(Character.isLetterOrDigit(c) || c == '-')) {
            cleanedStr.deleteCharAt(i--);
            continue;
        }
        if (i > 0 && c == '-' && cleanedStr.charAt(i - 1) == '-')
            cleanedStr.deleteCharAt(i--);
    }

    if (!Character.isLetterOrDigit(cleanedStr.charAt(0))) {
        cleanedStr.deleteCharAt(0);
    }

    if (cleanedStr.length() > 0 && !Character.isLetterOrDigit(cleanedStr.charAt(cleanedStr.length() - 1))) {
        cleanedStr.deleteCharAt(cleanedStr.length() - 1);
    }

    return cleanedStr.toString().toLowerCase().replaceAll("-", "_");
}

From source file:org.lwes.util.NumberCodec.java

/**
 * Write a number in unsigned hexadecimal form, padding with zeroes,
 * with a fixed result size.  Extra opening "f"'s are removed.
 *
 * @param num      the number to convert
 * @param numBytes the number of bytes to write (each is two hex digits)
 * @param buf      the StringBuffer into which to write
 *///from  w  w w  .ja v  a  2  s  .c  o m
private static void writeHexString(long num, int numBytes, StringBuffer buf) {
    final int startLen = buf.length();
    int numNibbles = numBytes << 1;
    int pos = startLen + numNibbles;
    buf.setLength(pos);
    while (numNibbles != 0) {
        --pos;
        final byte masked = (byte) (num & 0xf);
        buf.setCharAt(pos, hexCharMap[masked]);
        num >>>= 4;
        --numNibbles;
    }
}

From source file:org.exoplatform.services.cms.impl.Utils.java

/**
 * Clean string.//from ww w  .  j  a va  2 s .c  o  m
 *
 * @param str the str
 *
 * @return the string
 */
public static String cleanString(String str) {
    Transliterator accentsconverter = Transliterator
            .getInstance("Latin; NFD; [:Nonspacing Mark:] Remove; NFC;");
    str = accentsconverter.transliterate(str);
    //the character ? seems to not be changed to d by the transliterate function
    StringBuffer cleanedStr = new StringBuffer(str.trim());
    // delete special character
    for (int i = 0; i < cleanedStr.length(); i++) {
        char c = cleanedStr.charAt(i);
        if (c == ' ') {
            if (i > 0 && cleanedStr.charAt(i - 1) == '-') {
                cleanedStr.deleteCharAt(i--);
            } else {
                c = '-';
                cleanedStr.setCharAt(i, c);
            }
            continue;
        }
        if (i > 0 && !(Character.isLetterOrDigit(c) || c == '-')) {
            cleanedStr.deleteCharAt(i--);
            continue;
        }
        if (i > 0 && c == '-' && cleanedStr.charAt(i - 1) == '-')
            cleanedStr.deleteCharAt(i--);
    }
    while (StringUtils.isNotEmpty(cleanedStr.toString()) && !Character.isLetterOrDigit(cleanedStr.charAt(0))) {
        cleanedStr.deleteCharAt(0);
    }
    String clean = cleanedStr.toString().toLowerCase();
    if (clean.endsWith("-")) {
        clean = clean.substring(0, clean.length() - 1);
    }

    return clean;
}

From source file:org.apache.jasper.compiler.JspUtil.java

/**
 * Compute the canonical name from a Class instance.  Note that a
 * simple replacment of '$' with '.' of a binary name would not work,
 * as '$' is a legal Java Identifier character.
 * @param c A instance of java.lang.Class
 * @return  The canonical name of c.//  w  w  w. j av a  2  s  . c o m
 */
public static String getCanonicalName(Class c) {

    String binaryName = c.getName();
    c = c.getDeclaringClass();

    if (c == null) {
        return binaryName;
    }

    StringBuffer buf = new StringBuffer(binaryName);
    do {
        buf.setCharAt(c.getName().length(), '.');
        c = c.getDeclaringClass();
    } while (c != null);

    return buf.toString();
}