Example usage for java.lang StringBuilder replace

List of usage examples for java.lang StringBuilder replace

Introduction

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

Prototype

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

Source Link

Usage

From source file:org.fao.fenix.wds.core.utils.olap.OLAPWrapper.java

public static StringBuilder cleanJSON(StringBuilder sb) {

    String s = "{},";
    while ((sb.indexOf(s)) > -1)
        sb.replace(sb.indexOf(s), s.length() + sb.indexOf(s), "");

    s = "\"v\":[{}]";
    while ((sb.indexOf(s)) > -1)
        sb.replace(sb.indexOf(s), s.length() + sb.indexOf(s), "");

    s = ",}";/*w ww  . ja  v a2s .  c om*/
    while ((sb.indexOf(s)) > -1)
        sb.replace(sb.indexOf(s), s.length() + sb.indexOf(s), "}");

    int idx_1 = -1;
    int idx_2 = -1;
    for (int i = 0; i < sb.length(); i++) {
        if (sb.charAt(i) == '{')
            idx_1 = i;
        if (sb.charAt(i) == '}')
            idx_2 = i;
        if (idx_1 > -1 && idx_2 > -1) {
            int count = 0;
            for (int j = idx_1; j < idx_2; j++)
                if (sb.charAt(j) == '"')
                    count++;
            if (count == 4) {
                for (int z = idx_2; z >= idx_1; z--) {
                    sb.setCharAt(z, ' ');
                }
            }
            i = 1 + idx_2;
            idx_1 = -1;
            idx_2 = -1;
        }
    }

    s = " , ";
    while ((sb.indexOf(s)) > -1)
        sb.replace(sb.indexOf(s), s.length() + sb.indexOf(s), "");

    s = " ,{";
    while ((sb.indexOf(s)) > -1)
        sb.replace(sb.indexOf(s), s.length() + sb.indexOf(s), "{");

    s = "}]}, ";
    while ((sb.indexOf(s)) > -1)
        sb.replace(sb.indexOf(s), s.length() + sb.indexOf(s), "}]}");

    idx_1 = -1;
    idx_2 = -1;
    for (int i = 0; i < sb.length(); i++) {
        if (sb.charAt(i) == '}')
            idx_1 = i;
        if (sb.charAt(i) == '{' && i > idx_1)
            idx_2 = i;
        if (idx_1 > -1 && idx_2 > -1 && idx_2 > idx_1) {
            String tmp = sb.substring(idx_1, 1 + idx_2);
            int blanks = 0;
            int commas = 0;
            for (int z = 0; z < tmp.length(); z++) {
                switch (tmp.charAt(z)) {
                case ' ':
                    blanks++;
                    break;
                case ',':
                    commas++;
                    break;
                }
            }
            if (blanks > 0 && commas == 0) {
                String s1 = sb.substring(0, idx_1);
                String s2 = sb.substring(1 + idx_2);
                sb = new StringBuilder(s1 + "},{" + s2);
            }
            i = 1 + idx_2;
            idx_1 = -1;
            idx_2 = -1;
        }
    }

    s = "{}";
    while ((sb.indexOf(s)) > -1)
        sb.replace(sb.indexOf(s), s.length() + sb.indexOf(s), "");

    s = "},]";
    while ((sb.indexOf(s)) > -1)
        sb.replace(sb.indexOf(s), s.length() + sb.indexOf(s), "}]");

    return sb;
}

From source file:Main.java

/**
 * Sanitizes the XML represented by {@code xml} by replacing dirty header XML
 * snippets in the {@code dirtyXmlMap} map with clean header XML snippets from
 * the {@code cleanXmlMap} map.//from   w  w  w  . j  a v a  2  s  . c  om
 *
 * @param xml the XML to sanitize
 * @param dirtyXmlMap a map of tag name to dirty XML header
 * @param cleanXmlMap a map of tag name to clean XML header
 * @return a sanitized copy of the XML with all sensitive tags masked
 */
private static String sanitizeXml(StringBuilder xml, Map<String, String> dirtyXmlMap,
        Map<String, String> cleanXmlMap) {
    for (Entry<String, String> cleanXml : cleanXmlMap.entrySet()) {
        String dirtyXml = dirtyXmlMap.get(cleanXml.getKey());
        String endTag = cleanXml.getKey() + ">";
        int startIndex = xml.indexOf(dirtyXml.split(" ")[0]);
        int endIndex = xml.lastIndexOf(endTag) + endTag.length();
        xml = xml.replace(startIndex, endIndex, cleanXml.getValue());
    }
    return xml.toString();
}

From source file:org.apache.lucene.search.SearchUtil.java

public static String removeDirty(String contents, String startStr, String endStr, int strLen) {
    StringBuilder sb = new StringBuilder(contents);
    while (true) {
        if (sb.indexOf(startStr) != -1 && sb.indexOf(endStr) != -1) {
            int start = sb.indexOf(startStr);
            int end = sb.indexOf(endStr);
            sb.replace(start, end + strLen, "");
        } else {//  ww  w  . j a va 2  s  .  co  m
            break;
        }
    }
    return sb.toString();
}

From source file:com.mongodb.hadoop.util.MongoSplitter.java

private static MongoURI getNewURI(MongoURI originalUri, String newServerUri, Boolean slaveok) {

    String originalUriString = originalUri.toString();
    originalUriString = originalUriString.substring(MongoURI.MONGODB_PREFIX.length());

    // uris look like: mongodb://fred:foobar@server1[,server2]/path?options

    int serverEnd = -1;
    int serverStart = 0;

    int idx = originalUriString.lastIndexOf("/");
    if (idx < 0) {
        serverEnd = originalUriString.length();
    } else {//from  w  ww . j a va  2 s .  com
        serverEnd = idx;
    }
    idx = originalUriString.indexOf("@");

    if (idx > 0) {
        serverStart = idx + 1;
    }
    StringBuilder sb = new StringBuilder(originalUriString);
    sb.replace(serverStart, serverEnd, newServerUri);
    if (slaveok != null) {
        //If uri already contains options append option to end of uri.
        //This will override any slaveok option already in the uri
        if (originalUriString.contains("?"))
            sb.append("&slaveok=").append(slaveok);
        else
            sb.append("?slaveok=").append(slaveok);
    }
    String ans = MongoURI.MONGODB_PREFIX + sb.toString();
    log.debug("getNewURI(): original " + originalUri + " new uri: " + ans);
    return new MongoURI(ans);
}

From source file:Main.java

/**
 * Method reemplazaTextoDeLaEtiqueta. Dado una etiqueta con un contenido, lo reemplaza en todas las apariciones por uno nuevo en el documento dado
 * @param etiqueta Etiqueta a la que queremos reemplazarle el contenido. La etiqueta debe corresponderse con un item
 * @param contenidoViejo Contenido que queremos reemplazar
 * @param contenidoNuevo Nuevo contenido a poner en la etiqueta indicada
 * @param documento Documento con la etiqueta y contenido a reemplazar
 * @return String Documento con el contenido reemplazado
 *///w  w  w  .j  a v a 2 s. com
public static String reemplazaTextoDeLaEtiqueta(String etiqueta, String contenidoViejo, String contenidoNuevo,
        String documento) {
    StringBuilder st = new StringBuilder(documento);
    String cadenaVieja = (new StringBuilder("<").append(etiqueta).append(">").append(contenidoViejo)
            .append("</").append(etiqueta).append(">")).toString();
    int posIni = documento.indexOf(cadenaVieja);
    while (posIni != -1) {
        String cadenaNueva = (new StringBuilder("<").append(etiqueta).append(">").append(contenidoNuevo)
                .append("</").append(etiqueta).append(">")).toString();
        st.replace(posIni, posIni + cadenaVieja.length(), cadenaNueva);
        posIni = st.indexOf(cadenaVieja);
        if (cadenaVieja.equals(cadenaNueva))
            posIni = -1;
    }
    return st.toString();
}

From source file:org.apache.lucene.search.SearchUtil.java

public static String removeSecureContents(String contents) {
    StringBuilder sb = new StringBuilder(contents);
    while (true) {
        if (sb.indexOf("$SECURE_START$") != -1 && sb.indexOf("$SECURE_END$") != -1) {
            int start = sb.indexOf("$SECURE_START$");
            int end = sb.indexOf("$SECURE_END$");
            sb.replace(start, end + "$SECURE_END$".length(), "");
        } else {/*  w  ww .j a  v  a  2  s.  c om*/
            break;
        }
    }
    return sb.toString();
}

From source file:dhr.uploadtomicrobit.FirmwareGenerator.java

/**
* Utility method to replace the string from StringBuilder.
* @param sb          the StringBuilder object.
* @param toReplace   the String that should be replaced.
* @param replacement the String that has to be replaced by.
* 
*//*from   w  w w . j  av  a2  s .c o m*/
public static void replaceString(StringBuilder sb, String toReplace, String replacement) {

    System.out.println("Pattern : " + toReplace + " Len: " + toReplace.length());

    int index = sb.indexOf(toReplace);

    System.out.println("Index of pattern: " + index);

    if (index <= 0) {
        System.out.println("Failed to see correct replacement pattern in firmware file");
        sb.replace(0, 6, "Invalid");
        return;
    }
    int end = index + toReplace.length();

    System.out.println("Index of pattern: " + index);
    System.out.println("End of pattern: " + end);
    System.out.println("New Pattern : " + replacement);

    sb.replace(index, end, replacement);

    //String tail = new String(sb.substring(end));   // tail conmponent

}

From source file:com.melani.utils.ProjectHelpers.java

public static String parsearCaracteresEspecialesXML1(String xmlaParsear) {
    String xml = "No paso Nada";
    StringBuilder sb = null;
    try {//  www. j  a  va 2  s .  c  o m

        sb = new StringBuilder(xmlaParsear);
        if (xmlaParsear.indexOf("<item>") != -1) {
            xml = StringEscapeUtils.escapeXml10(
                    xmlaParsear.substring(xmlaParsear.indexOf("nes>") + 4, xmlaParsear.indexOf("</obse")));
            sb.replace(sb.indexOf("nes>") + 4, sb.indexOf("</obse"), xml);
        }
        if (xmlaParsear.indexOf("<Domicilio>") != -1) {
            xml = StringEscapeUtils.escapeXml10(
                    xmlaParsear.substring(xmlaParsear.indexOf("mes>") + 4, xmlaParsear.indexOf("</det1")));
            sb.replace(sb.indexOf("mes>") + 4, sb.indexOf("</det1"), xml);
        }
        xml = sb.toString();

    } catch (Exception e) {
        xml = "Error";
        logger.error("Error en metodo parsearCaracteresEspecialesXML1 " + e.getLocalizedMessage());
    } finally {
        return xml;
    }
}

From source file:de.uni_potsdam.hpi.asg.logictool.helper.BDDHelper.java

private static String formatOrGate(BDD bdd, Netlist netlist) {
    StringBuilder str = new StringBuilder();
    str.append("(");
    for (NetlistVariable var : getVars(bdd, netlist)) {
        if (!isPos(bdd, var)) {
            str.append("!");
        }//w  w  w. j  a va 2 s .  c om
        str.append(var.getName() + "+");
    }
    str.replace(str.length() - 1, str.length(), ")");
    return str.toString();
}

From source file:fr.landel.utils.commons.StringFormatUtils.java

private static StringBuilder replaceAndClear(final Set<Group> groups, final StringBuilder sb) {
    groups.stream().sorted(Group.COMPARATOR.desc()).forEachOrdered((g) -> {
        if (g.remove) {
            sb.replace(g.start, g.end, EMPTY);
        } else {//  ww w.  ja v  a 2  s. c  o m
            g.asterisk = false;
            sb.replace(g.start, g.end, g.toString());
        }
    });

    return sb;
}