Example usage for java.lang StringBuffer replace

List of usage examples for java.lang StringBuffer replace

Introduction

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

Prototype

@Override
public synchronized StringBuffer replace(int start, int end, String str) 

Source Link

Usage

From source file:org.jboss.dashboard.commons.text.StringUtil.java

/**
 * Replaces the characters in all substrings of a String with characters in
 * the specified array of substrings.// www.  j  ava  2  s  . c  o m
 *
 * @param str original string
 * @param in  substrings to search in the orig String
 * @param out array of new substrings
 * @return if the parameters are correct returns the new string; otherwise
 *         if some of the parameters is null returns the original string
 */
public static String replaceAll(String str, String[] in, String[] out) {
    if (str == null || in == null || out == null || in.length != out.length) {
        return str; // for the sake of robustness
    }

    StringBuffer buffer = new StringBuffer(str);

    int idx = 0;
    for (int i = 0; i < in.length; i++) {
        idx = 0;
        while ((idx = indexOf(in[i], idx, buffer)) != -1) {
            buffer.replace(idx, idx + in[i].length(), out[i]);
        }
    }
    return buffer.toString();
}

From source file:org.kuali.kfs.pdp.web.struts.BatchAction.java

/**
 * This method build a string list of error message keys out of the error map in GlobalVariables
 * /*from  ww w. j  av  a 2  s .  co m*/
 * @return a String representing the list of error message keys
 */
private String buildErrorMesageKeyList() {
    MessageMap errorMap = GlobalVariables.getMessageMap();
    StringBuffer errorList = new StringBuffer();

    for (String errorKey : (List<String>) errorMap.getPropertiesWithErrors()) {
        for (ErrorMessage errorMessage : (List<ErrorMessage>) errorMap.getMessages(errorKey)) {

            errorList.append(errorMessage.getErrorKey());
            errorList.append(PdpParameterConstants.ERROR_KEY_LIST_SEPARATOR);
        }
    }
    if (errorList.length() > 0) {
        errorList.replace(errorList.lastIndexOf(PdpParameterConstants.ERROR_KEY_LIST_SEPARATOR),
                errorList.lastIndexOf(PdpParameterConstants.ERROR_KEY_LIST_SEPARATOR)
                        + PdpParameterConstants.ERROR_KEY_LIST_SEPARATOR.length(),
                "");
    }

    return errorList.toString();
}

From source file:org.jboss.dashboard.commons.text.StringUtil.java

/**
 * Remove all the ocurrences of a group of substring from a string
 *
 * @param str    original string//w  ww . j a v  a2s .  c om
 * @param substr array of substrings to remove from the orig String
 * @return an string without all the ocurrences of the substring passed
 *         as a parameter
 */
public static String removeAll(String str, String[] substr) {

    if (str == null || substr == null) {
        return str; // for the sake of robustness
    }

    StringBuffer buffer = new StringBuffer(str);
    int idx = 0;
    for (int i = 0; i < substr.length; i++) {
        idx = 0;
        while ((idx = indexOf(substr[i], idx, buffer)) != -1) {
            buffer.replace(idx, idx + substr[i].length(), "");
        }
    }
    return buffer.toString();
}

From source file:io.lightlink.output.JSONResponseStream.java

private void writeEscapedString(String valueStr) {
    char[] chars = valueStr.toCharArray();
    StringBuffer sb = new StringBuffer(valueStr);
    for (int i = chars.length - 1; i >= 0; i--) {
        char ch = chars[i];
        switch (ch) {
        case '"':
            sb.replace(i, i + 1, STR_QUOT);
            break;
        case '\\':
            sb.replace(i, i + 1, STR_BACKSLASH);
            break;
        case '\b':
            sb.replace(i, i + 1, STR_B);
            break;
        case '\f':
            sb.replace(i, i + 1, STR_F);
            break;
        case '\n':
            sb.replace(i, i + 1, STR_N);
            break;
        case '\r':
            sb.replace(i, i + 1, STR_R);
            break;
        case '\t':
            sb.replace(i, i + 1, STR_T);
            break;
        case '/':
            sb.replace(i, i + 1, STR_SLASH);
            break;
        default:/*w w w  .  jav a 2 s. co  m*/
            //Reference: http://www.unicode.org/versions/Unicode5.1.0/
            if ((ch >= '\u0000' && ch <= '\u001F') || (ch >= '\u007F' && ch <= '\u009F')
                    || (ch >= '\u2000' && ch <= '\u20FF')) {
                StringBuilder encoded = new StringBuilder();
                String ss = Integer.toHexString(ch).toUpperCase();
                encoded.append(STR_SLASH_U);
                for (int k = 0; k < 4 - ss.length(); k++) {
                    encoded.append('0');
                }
                encoded.append(ss.toUpperCase());
                sb.replace(i, i + 1, encoded.toString());
            }
        }
    }
    ByteBuffer buffer = CHARSET.encode(sb.toString());
    write(buffer.array(), buffer.remaining());

}

From source file:com.ancientprogramming.fixedformat4j.format.impl.FixedFormatManagerImpl.java

private void appendData(StringBuffer result, Character paddingChar, Integer offset, String data) {
    int zeroBasedOffset = offset - 1;
    while (result.length() < zeroBasedOffset) {
        result.append(paddingChar);//w w  w. j a va  2 s  . c o m
    }
    int length = data.length();
    if (result.length() < zeroBasedOffset + length) {
        result.append(StringUtils.leftPad("", (zeroBasedOffset + length) - result.length(), paddingChar));
    }
    result.replace(zeroBasedOffset, zeroBasedOffset + length, data);
}

From source file:org.nuxeo.connect.packages.dependencies.DependencyResolution.java

private StringBuffer append(StringBuffer sb, Map<String, Version> pkgMap, String title) {
    if (!pkgMap.isEmpty()) {
        append(sb, title, pkgMap.size());
        for (String pkgName : pkgMap.keySet()) {
            sb.append(pkgName);/*from   w  w w. j  a v a2  s  . co m*/
            sb.append(":");
            sb.append(pkgMap.get(pkgName).toString());
            sb.append(", ");
        }
        sb.replace(sb.length() - 2, sb.length(), "\n");
    }
    return sb;
}

From source file:org.regenstrief.hl7.util.HL7IO.java

static public StringBuffer replaceNthField(final StringBuffer sb, final char ch, final int n,
        final String newval) {// zero based
    int idx = -1, cnt = (n > 0 ? n : 0);
    // Find the n'th delim
    //Utl.dp("replaceNthString:",n);
    if (n < 0) {
        return sb;
    }/*from w  w  w .  ja v a  2s  .c  om*/
    for (int prev = 0; cnt > 0; prev = idx + 1) {
        idx = indexOf(sb, ch, prev);
        //Utl.dp("cnt:",cnt,"idx:",idx);
        if (idx < 0) {
            break;
        }
        cnt--;
        //Utl.dis("sofar",idx,s.substring(prev,idx));
    }
    //Utl.dp("End search for nth idx:",idx," cnt = ",cnt);
    if (cnt > 0) {
        /* then we need to pad input */
        //Utl.dp("pad by cnt:",cnt);
        //Utl.dp("prev:",prev);
        while (cnt-- > 0) {
            sb.append(ch);
        }
        // The new field is just appended
        sb.append(newval);
        // We are done
        return sb;
    }
    //Utl.dp("cnt:",cnt,"idx:",idx);
    // Is there a next delimiter
    final int closer = indexOf(sb, ch, idx + 1);
    //  Utl.dp("closer = ",closer);
    if (closer > 0) {
        // Yup, replace the range
        sb.replace(idx + 1, closer, newval);
    } else {
        // No closer
        sb.replace(idx + 1, sb.length(), newval);
    }
    return sb;
}

From source file:de.iteratec.iteraplan.businesslogic.service.SavedQueryServiceImpl.java

private void checkQueryFormsAttributeTypeNotNull(List<QueryFormXML> queryForms, MutableBoolean tmp) {
    for (QueryFormXML queryFormXML : queryForms) {
        for (QFirstLevelXML qFirstLevelXML : queryFormXML.getQueryUserInput().getQueryFirstLevels()) {
            ArrayList<QPartXML> qPartsList = new ArrayList<QPartXML>();

            for (QPartXML qPart : qFirstLevelXML.getQuerySecondLevels()) {
                if (qPart.getChosenAttributeStringId() != null) {
                    Integer id = BBAttribute.getIdByStringId(qPart.getChosenAttributeStringId());
                    if (attributeTypeDAO.loadObjectByIdIfExists(id) == null && isAT(id)) {
                        String attributeType = BBAttribute
                                .getTypeByStringId(qPart.getChosenAttributeStringId());
                        StringBuffer attributeStringBuffer = new StringBuffer(
                                qPart.getChosenAttributeStringId().replace(id.toString(), "-1"));
                        attributeStringBuffer.replace(0, attributeType.length(),
                                BBAttribute.BLANK_ATTRIBUTE_TYPE);
                        qPart.setChosenAttributeStringId(attributeStringBuffer.toString());
                        tmp.setValue(true);
                    }//from  ww  w .j ava  2s  . c  o  m
                }
                qPartsList.add(qPart);
            }
            qFirstLevelXML.setQuerySecondLevels(qPartsList);
        }
    }
}

From source file:com.doculibre.constellio.wicket.panels.spellchecker.SpellCheckerPanel.java

@SuppressWarnings("unchecked")
private String getSuggestedSearchAsString() {
    StringBuffer sb = new StringBuffer();
    ListOrderedMap suggestedSearch = (ListOrderedMap) getModelObject();
    for (String motOriginal : (Set<String>) suggestedSearch.keySet()) {
        List<String> suggestedWords = (List<String>) suggestedSearch.get(motOriginal);
        if (suggestedWords == null || suggestedWords.isEmpty()) {
            sb.append(motOriginal);/*from  ww  w  . j  a v a 2 s . c  o  m*/
        } else {
            // First word
            sb.append(suggestedWords.get(0));
        }
        sb.append(" ");
    }
    if (sb.length() > 0) {
        // Supprimer le dernier espace
        sb.replace(sb.length() - 1, sb.length(), "");
    }
    return sb.toString();
}

From source file:org.jboss.dashboard.commons.text.StringUtil.java

/**
 * Replaces a character of a String with characters in the specified
 * new substring.//from w w  w.j  a va  2  s. c om
 *
 * @param origStr string that contains the replaceable substring
 * @param oldChar character to search in the orig String
 * @param newStr  substring of the new characters
 * @return if the parameters are correct returns the new string; otherwise
 *         if origStr parameter is null returns null. If newstr parameter is
 *         null returns origStr parameter
 */
public static String replaceAll(String origStr, char oldChar, String newStr) {
    if (origStr == null) {
        return null;
    } else if (newStr == null) {
        return origStr;
    }

    StringBuffer buffer = new StringBuffer(origStr);
    int index = origStr.indexOf(oldChar);
    int replIndex;
    int insertPadding = 0;
    int padding = newStr.length() - 1;
    while (index > -1) {
        replIndex = index + insertPadding;
        buffer.replace(replIndex, (replIndex + 1), newStr);
        index++;
        index = origStr.indexOf(oldChar, index);
        insertPadding += padding;
    }
    return buffer.toString();
}