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:de.mpg.escidoc.pubman.multipleimport.ImportProcess.java

public static void replace(String target, String replacement, StringBuilder builder) {
    int indexOfTarget = -1;
    while ((indexOfTarget = builder.indexOf(target)) >= 0) {
        builder.replace(indexOfTarget, indexOfTarget + target.length(), replacement);
    }/*from  w w w  .  j  a va2s.  c  om*/
}

From source file:com.easysoft.build.utils.PatchUtil.java

/**
 * ??/* www.j a  v  a 2 s. c  o m*/
 * @param sb 
 * @param sectionName ????
 * @param content  ??
 */
public static void replaceSection(StringBuilder sb, String sectionName, String content) {
    String prefix = "\r\n-- -" + sectionName + "-\r\n";
    String suffix = "\r\n-- =" + sectionName + "=\r\n";
    int sIndex = sb.indexOf(prefix);
    int eIndex = sb.indexOf(suffix);
    if (sIndex != -1 && eIndex != -1 && sIndex < eIndex) {
        sb.replace(sIndex, eIndex + suffix.length(), "");
    }
    if (content.length() > 0) {
        sb.append(prefix);
        content = content.replaceAll("", ",").replaceAll("", ";").replaceAll("", "'")
                .replaceAll("", "'").replaceAll("", ".");
        sb.append(content);
        sb.append(suffix);
    }

}

From source file:org.jboss.errai.codegen.util.CDIAnnotationUtils.java

public static AnnotationPropertyAccessor createDynamicSerializer(
        final Class<? extends Annotation> annotationType) {
    final AnnotationPropertyAccessorBuilder builder = AnnotationPropertyAccessorBuilder.create();

    final Collection<Method> annoAttrs = CDIAnnotationUtils.getAnnotationAttributes(annotationType);
    for (final Method attr : annoAttrs) {
        builder.with(attr.getName(), anno -> {
            try {
                final String retVal;
                final Function<Object, String> toString = componentToString(
                        attr.getReturnType().isArray() ? attr.getReturnType().getComponentType()
                                : attr.getReturnType());
                if (attr.getReturnType().isArray()) {
                    final StringBuilder sb = new StringBuilder();
                    final Object[] array = (Object[]) attr.invoke(anno);
                    sb.append("[");
                    for (final Object obj : array) {
                        sb.append(toString.apply(obj)).append(",");
                    }//from  ww  w  .j  a v  a2  s  . co m
                    sb.replace(sb.length() - 1, sb.length(), "]");
                    retVal = sb.toString();
                } else {
                    retVal = toString.apply(attr.invoke(anno));
                }
                return retVal;
            } catch (final Exception e) {
                throw new RuntimeException(String.format("Could not access '%s' property while serializing %s.",
                        attr.getName(), anno.annotationType()), e);
            }
        });
    }

    return builder.build();
}

From source file:Main.java

private static String quoteCharacters(String s) {
    StringBuilder result = null;
    for (int i = 0, max = s.length(), delta = 0; i < max; i++) {
        char c = s.charAt(i);
        String replacement = null;

        if (c == '&') {
            replacement = "&amp;";
        } else if (c == '<') {
            replacement = "&lt;";
        } else if (c == '\r') {
            replacement = "&#13;";
        } else if (c == '>') {
            replacement = "&gt;";
        } else if (c == '"') {
            replacement = "&quot;";
        } else if (c == '\'') {
            replacement = "&apos;";
        }/*from   w  ww .  j av  a 2  s . c om*/

        if (replacement != null) {
            if (result == null) {
                result = new StringBuilder(s);
            }
            result.replace(i + delta, i + delta + 1, replacement);
            delta += (replacement.length() - 1);
        }
    }
    if (result == null) {
        return s;
    }
    return result.toString();
}

From source file:com.jaspersoft.jasperserver.core.util.StringUtil.java

/**
 * Replaces all occurrences of a string in a buffer with another.
 *
 * @param buf String buffer to act on//www .ja  va 2  s .c  om
 * @param start Ordinal within <code>find</code> to start searching
 * @param find String to find
 * @param replace String to replace it with
 * @return The string buffer
 */
public static StringBuilder replace(StringBuilder buf, int start, String find, String replace) {
    // Search and replace from the end towards the start, to avoid O(n ^ 2)
    // copying if the string occurs very commonly.
    int findLength = find.length();
    if (findLength == 0) {
        // Special case where the seek string is empty.
        for (int j = buf.length(); j >= 0; --j) {
            buf.insert(j, replace);
        }
        return buf;
    }
    int k = buf.length();
    while (k > 0) {
        int i = buf.lastIndexOf(find, k);
        if (i < start) {
            break;
        }
        buf.replace(i, i + find.length(), replace);
        // Step back far enough to ensure that the beginning of the section
        // we just replaced does not cause a match.
        k = i - findLength;
    }
    return buf;
}

From source file:org.apache.wiki.util.TextUtil.java

/**
 *  Replaces a part of a string with a new String.
 *
 *  @param start Where in the original string the replacing should start.
 *  @param end Where the replacing should end.
 *  @param orig Original string.  Null is safe.
 *  @param text The new text to insert into the string.
 *  @return The string with the orig replaced with text.
 *//*from w  w  w.  j  av a 2  s.c o  m*/
public static String replaceString(String orig, int start, int end, String text) {
    if (orig == null)
        return null;

    StringBuilder buf = new StringBuilder(orig);
    buf.replace(start, end, text);
    return buf.toString();
}

From source file:org.apache.torque.generator.outlet.java.JavadocOutlet.java

/**
 * Removes the trailing characters from a string builder.
 * The characters to be removed are passed in as parameter.
 *
 * @param stringBuilder the string builder to remove the end from, not null.
 * @param removeChars The characters to remove if they appear at the end,
 *        not null./*ww  w. j  a  va2  s.c om*/
 */
static void removeEnd(StringBuilder stringBuilder, String removeChars) {
    Set<Character> removeCharSet = new HashSet<Character>();
    for (char character : removeChars.toCharArray()) {
        removeCharSet.add(character);
    }
    int index = stringBuilder.length();
    while (index > 0) {
        if (!removeCharSet.contains(stringBuilder.charAt(index - 1))) {
            break;
        }
        index--;
    }
    // index is now last char in String which does not match pattern
    // maybe -1 if all the string matches or the input is empty
    stringBuilder.replace(index, stringBuilder.length(), "");
}

From source file:com.magic.util.StringUtil.java

/**
 * Replaces all occurrences of oldString in mainString with newString
 * @param mainString The original string
 * @param oldString The string to replace
 * @param newString The string to insert in place of the old
 * @return mainString with all occurrences of oldString replaced by newString
 *//*from   w  ww .  j ava2 s . co  m*/
public static String replaceString(String mainString, String oldString, String newString) {
    if (mainString == null) {
        return null;
    }
    if (oldString == null || oldString.length() == 0) {
        return mainString;
    }
    if (newString == null) {
        newString = "";
    }

    int i = mainString.lastIndexOf(oldString);

    if (i < 0)
        return mainString;

    StringBuilder mainSb = new StringBuilder(mainString);

    while (i >= 0) {
        mainSb.replace(i, i + oldString.length(), newString);
        i = mainString.lastIndexOf(oldString, i - 1);
    }
    return mainSb.toString();
}

From source file:net.firejack.platform.core.utils.db.DBUtils.java

private static String populateInsertQuery(TablesMapping mapping) {
    Map<Column, Column> columnMapping = mapping.getColumnMapping();
    StringBuilder insertQuery = new StringBuilder("insert into ");
    insertQuery.append(mapping.getTargetTable().getName()).append('(');
    Set<Map.Entry<Column, Column>> columnEntries = columnMapping.entrySet();
    for (Map.Entry<Column, Column> entry : columnEntries) {
        Column targetColumn = entry.getValue();
        insertQuery.append(targetColumn.getName()).append(',');
    }//from   w w  w .  j  a v  a 2s .com
    if (!columnMapping.isEmpty()) {
        insertQuery.replace(insertQuery.length() - 1, insertQuery.length(), "");
    }
    insertQuery.append(") values (");
    for (int i = 0; i < columnEntries.size(); i++) {
        insertQuery.append(i == 0 ? '?' : ", ?");
    }
    insertQuery.append(')');
    return insertQuery.toString();
}

From source file:Main.java

public static String ponTextosDeLaEtiqueta(String etiqueta, String nuevoContenido, String documento) {
    int posicionEtiqueta = documento.indexOf(etiqueta);
    StringBuilder st = new StringBuilder(documento);
    while (posicionEtiqueta != -1) {
        if (!esUnaEtiquetaDeAperturaYCierre(etiqueta, posicionEtiqueta, documento)
                && esRealmenteUnaEtiquetaDeApertura(etiqueta, posicionEtiqueta, documento)) {
            int posCierreAperturaEtiqueta = documento.indexOf('>', posicionEtiqueta);
            int posAperturaCierreEtiqueta = documento.indexOf("</", posCierreAperturaEtiqueta);
            boolean esRealmenteUnCierreDeEtiqueta = false;
            int i = 1;
            while (!esRealmenteUnCierreDeEtiqueta) {
                if (documento.charAt(posAperturaCierreEtiqueta - i) != '\\')
                    esRealmenteUnCierreDeEtiqueta = true;
                else
                    i++;/*from  ww w . jav a  2  s . c  om*/
            }
            st.replace(posCierreAperturaEtiqueta + 1, posAperturaCierreEtiqueta, nuevoContenido);
            posicionEtiqueta = posAperturaCierreEtiqueta + etiqueta.length(); //para avanzar mas rapido
        }
        posicionEtiqueta = documento.indexOf(etiqueta, posicionEtiqueta + 1);
    }
    return st.toString();
}