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:fr.landel.utils.io.FileUtils.java

/**
 * Convert all newline characters into Mac OS newlines.
 * /*  www .ja va  2  s .c o m*/
 * @param input
 *            The text to convert
 * @return The text converted
 */
public static StringBuilder convertToMacOS(final StringBuilder input) {
    final StringBuilder output = new StringBuilder(input);

    int pos = 0;
    while ((pos = output.indexOf(CRLF, pos)) > -1) {
        output.replace(pos, pos + 2, LFCR);
        pos++;
    }
    pos = 0;
    while ((pos = output.indexOf(C, pos)) > -1) {
        if (pos == 0 || output.charAt(pos - 1) != LF) {
            output.replace(pos, pos + 1, LFCR);
        }
        pos++;
    }
    pos = 0;
    final int len = output.length();
    while ((pos = output.indexOf(L, pos)) > -1) {
        if (pos == len - 1 || output.charAt(pos + 1) != CR) {
            output.replace(pos, pos + 1, LFCR);
        }
        pos++;
    }

    return output;
}

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

private static ResultSet selectDataFromSource(Connection sourceConnection, TablesMapping mapping)
        throws SQLException {
    Map<Column, Column> columnMapping = mapping.getColumnMapping();
    StringBuilder selectQuery = new StringBuilder("select ");
    for (Map.Entry<Column, Column> columnEntry : columnMapping.entrySet()) {
        Column sourceColumn = columnEntry.getKey();
        selectQuery.append(sourceColumn.getName()).append(',');
    }/*w  ww  . jav  a  2  s.  c  o  m*/
    if (!columnMapping.isEmpty()) {
        selectQuery.replace(selectQuery.length() - 1, selectQuery.length(), "");
    }
    selectQuery.append(" from ").append(mapping.getSourceTable().getName());
    String sql = selectQuery.toString();
    Statement statement = sourceConnection.createStatement(ResultSet.TYPE_FORWARD_ONLY,
            ResultSet.CONCUR_READ_ONLY);
    ResultSet rs = statement.executeQuery(sql);
    rs.setFetchSize(DEFAULT_BATCH_SIZE);
    return rs;
}

From source file:org.apache.ofbiz.base.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  a v  a 2  s  .  c  om*/
public static String replaceString(String mainString, String oldString, String newString) {
    if (mainString == null) {
        return null;
    }
    if (UtilValidate.isEmpty(oldString)) {
        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:marytts.server.MaryProperties.java

/**
 * From a path entry in the properties, create an expanded form.
 * Replace the string MARY_BASE with the value of property "mary.base";
 * replace all "/" and "\\" with the platform-specific file separator.
 *//*from   www  .j  a  v a  2s  .co m*/
private static String expandPath(String path) {
    final String MARY_BASE = "MARY_BASE";
    StringBuilder buf = null;
    if (path.startsWith(MARY_BASE)) {
        buf = new StringBuilder(maryBase());
        buf.append(path.substring(MARY_BASE.length()));
    } else {
        buf = new StringBuilder(path);
    }
    if (File.separator.equals("/")) {
        int i = -1;
        while ((i = buf.indexOf("\\")) != -1)
            buf.replace(i, i + 1, "/");
    } else if (File.separator.equals("\\")) {
        int i = -1;
        while ((i = buf.indexOf("/")) != -1)
            buf.replace(i, i + 1, "\\");
    } else {
        throw new Error("Unexpected File.separator: `" + File.separator + "'");
    }
    return buf.toString();
}

From source file:com.gargoylesoftware.htmlunit.util.StringUtils.java

/**
 * Escape the string to be used as attribute value.
 * Only {@code <}, {@code &} and {@code "} have to be escaped (see http://www.w3.org/TR/REC-xml/#d0e888).
 * @param attValue the attribute value/* w  ww. ja v  a2  s. c o m*/
 * @return the escaped value
 */
public static String escapeXmlAttributeValue(final String attValue) {
    final int len = attValue.length();
    StringBuilder sb = null;
    for (int i = len - 1; i >= 0; --i) {
        final char c = attValue.charAt(i);
        String replacement = null;
        if (c == '<') {
            replacement = "&lt;";
        } else if (c == '&') {
            replacement = "&amp;";
        } else if (c == '\"') {
            replacement = "&quot;";
        }

        if (replacement != null) {
            if (sb == null) {
                sb = new StringBuilder(attValue);
            }
            sb.replace(i, i + 1, replacement);
        }
    }

    if (sb != null) {
        return sb.toString();
    }
    return attValue;
}

From source file:be.fedict.eid.pkira.blm.model.mail.MailTemplateBean.java

public static final String unescapeHTML(String s) {
    StringBuilder builder = new StringBuilder(s);

    while (true) {
        int i = builder.indexOf("&");

        if (i > -1) {
            int j = builder.indexOf(";", i);
            if (j > i) {
                String temp = builder.substring(i, j + 1);
                String replacement = "?";
                for (String[] replacements : htmlEscape) {
                    if (replacements[0].equals(temp)) {
                        replacement = replacements[1];
                        break;
                    }//from   ww w  .ja  v  a 2s.c o  m
                }
                builder.replace(i, j + 1, replacement);
            } else {
                break;
            }
        } else {
            break;
        }
    }

    return builder.toString();
}

From source file:org.loklak.android.data.MessageEntry.java

private static List<String> extract(StringBuilder s, Pattern p, int g) {
    Matcher m = p.matcher(s.toString());
    List<String> l = new ArrayList<String>();
    while (m.find())
        l.add(m.group(g));/*from   ww w  .  j  ava 2 s .c o m*/
    for (String r : l) {
        int i = s.indexOf(r);
        s.replace(i, i + r.length(), "");
    }
    return l;
}

From source file:org.onosproject.yang.compiler.utils.io.impl.YangIoUtils.java

/**
 * Replaces the last occurrence of a string with a given string.
 *
 * @param valueString     string under operation
 * @param removalString   string to be replaced
 * @param replacingString string with which replacement is to be done
 * @return new string/*w  ww .jav a  2  s.c  o m*/
 */
public static String replaceLast(String valueString, String removalString, String replacingString) {
    StringBuilder stringBuilder = new StringBuilder(valueString);
    int index = valueString.lastIndexOf(removalString);
    if (index != -1) {
        stringBuilder.replace(index, index + 1, replacingString);
    } else {
        stringBuilder.append(NEW_LINE + EIGHT_SPACE_INDENTATION + UNUSED + OPEN_PARENTHESIS + ONE
                + CLOSE_PARENTHESIS + SEMI_COLON);
    }
    return stringBuilder.toString();

    // TODO remove generation of ENUM if there is no leaf node.
}

From source file:Main.java

/**
 * takes an escape'd XML string and converts it back to orginal text contents
 * /*from ww w .  ja v a  2  s  . c o  m*/
 * @param chars XML contents
 * @return String translation of XML contents
 **/
public static final String unescape(final String chars) {
    if (chars == null) {
        return "";
    }

    final StringBuilder res = new StringBuilder(chars);
    int i = res.indexOf("&");

    while (i != -1) {
        final int j = res.indexOf(";", i + 1);
        if (j == -1) {
            break;
        }
        final String entity = res.substring(i + 1, j);
        if (entity.startsWith("#")) {
            if ((entity.length() == 3) && (entity.charAt(1) == 'x')) {
                // http://www.w3.org/TR/1999/WD-xml-c14n-19991109.html
                // 5.2 Character Escaping
                switch (entity.charAt(2)) {
                case '9':
                    res.replace(i, j + 1, "\t");
                    break;
                case 'D':
                    res.replace(i, j + 1, "\r");
                    break;
                case 'A':
                    res.replace(i, j + 1, "\n");
                    break;
                }
            } else {
                try {
                    final int charCode = Integer.parseInt(entity.substring(1));
                    final char c = (char) charCode;
                    res.replace(i, j + 1, Character.toString(c));
                    i = i + 1;
                } catch (final NumberFormatException e) {
                    res.delete(i, j + 1);
                }
            }
        } else {
            final String textEntity = XML_ENTITY_MAP.get(entity);
            if (textEntity != null) {
                res.replace(i, j + 1, textEntity);
                i = i + textEntity.length();
            } else {
                res.delete(i, j + 1);
            }
        }
        i = res.indexOf("&", i);
    }

    return res.toString();
}

From source file:org.omegat.util.StaticUtils.java

private static void replaceGlobs(StringBuilder haystack, String needle, String replacement) {
    replacement = "\\E" + replacement + "\\Q";
    int current = 0;
    int globIndex = 0;
    while ((globIndex = haystack.indexOf(needle, current)) != -1) {
        haystack.replace(globIndex, globIndex + 1, replacement);
        current = globIndex + replacement.length();
    }/*from  ww w  . j a v  a 2  s .  c o  m*/
}