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:Main.java

public static String argumentTagToCmd(Element jobElement) {
    //        NodeList nl = jobElement.getChildNodes();
    //        for(int i=0;i<nl.getLength();i++)
    //        {//from   w  w  w.  ja v a2s  .c  o m
    //            System.out.println(nl.item(i).getTextContent());
    //        }
    //        System.exit(1);

    //        Node n = jobElement.getElementsByTagName("argument").item(0);
    String taskName = jobElement.getAttribute("name");
    //        String nodeString = nodeToString(n);
    //        nodeString = nodeString.replace("<"+n.getNodeName()+">", "");
    //        nodeString = nodeString.replace("</"+n.getNodeName()+">", "");
    //        nodeString = nodeString.trim();
    //        String[] lines = nodeString.split("\n");
    StringBuilder cmd = new StringBuilder(taskName).append(";");

    NodeList argList = jobElement.getElementsByTagName("argument").item(0).getChildNodes();
    for (int i = 0; i < argList.getLength(); i++) {
        Node c = argList.item(i);
        String cStr;
        if (c.getNodeName().equals("file")) {
            Element ec = (Element) c;
            cStr = (ec.getAttribute("name")).trim();
            if (!cStr.isEmpty()) {
                cmd.append(cStr).append(";");
            }
        } else {
            cStr = (c.getTextContent().trim());
            String[] cStrs = cStr.split("\\s+");
            for (String cs : cStrs) {
                cs = cs.trim();
                if (!cs.isEmpty()) {
                    cmd.append(cs).append(";");
                }
            }
        }

    }

    cmd.replace(cmd.length() - 1, cmd.length(), "");
    return cmd.toString();
}

From source file:org.jspringbot.keyword.expression.ELUtils.java

public static String replaceVars(String string) throws Exception {
    StringBuilder buf = new StringBuilder(string);
    Matcher matcher = PATTERN.matcher(buf);

    int startIndex = 0;
    while (startIndex < buf.length() && matcher.find(startIndex)) {
        String name = matcher.group(1);

        Object value = getVariables().getVariables().get(name);
        LOG.keywordAppender().appendProperty("Replacement EL Value ['" + name + "']", value);
        if (value == null) {
            value = robotVar(name);/*from  w  w w.j a  v  a 2s.c  om*/
            LOG.keywordAppender().appendProperty("Replacement Robot Value ['" + name + "']", value);
        }

        String strValue = String.valueOf(value);

        buf.replace(matcher.start(), matcher.end(), strValue);
        startIndex = matcher.start() + strValue.length();
    }

    LOG.keywordAppender().appendProperty(String.format("Replacement [%s]", string), buf.toString());

    return buf.toString();
}

From source file:org.opendaylight.didm.tools.utils.StringUtils.java

/**
 * A simple string formatter that replaces each occurrence of
 * <code>{}</code> in the format string with the string representation
 * of each of the subsequent, optional arguments.
 * For example://from   ww  w .java2s  .  co  m
 * <pre>
 *     StringUtils.format("{} = {}", "Foo", 123);
 *     // returns "Foo = 123"
 * </pre>
 *
 * @param fmt the format string
 * @param o arguments to be inserted into the output string
 * @return a formatted string
 */
public static String format(String fmt, Object... o) {
    if (fmt == null)
        throw new NullPointerException("null format string");
    if (o.length == 0)
        return fmt;

    // Format the message using the format string as the seed.
    // Stop either when the list of objects is exhausted or when
    // there are no other place-holder tokens.
    final int ftlen = FORMAT_TOKEN.length();
    int i = 0;
    int p = -1;
    String rep;
    StringBuilder sb = new StringBuilder(fmt);
    while (i < o.length && (p = sb.indexOf(FORMAT_TOKEN, p + 1)) >= 0) {
        rep = o[i] == null ? NULL_REP : o[i].toString();
        sb.replace(p, p + ftlen, rep);
        i++;
    }
    return sb.toString();
}

From source file:pl.nask.hsn2.normalizers.URLNormalizerUtils.java

static String normalizeSpaceEncoding(StringBuilder sb) {
    int i = -1;/*from   ww w  . ja  v a  2 s . co  m*/
    while ((i = findFirstMatch(sb, new String[] { "+" }, i + 1)) >= 0) {

        sb.replace(i, i + 1, " ");

    }
    return sb.toString();
}

From source file:br.msf.commons.text.HexUtils.java

/**
 * Puts a separator between groups of <tt>groupLen</tt> nibbles.
 * <p/>//w  ww  .  j a va2 s  . c  om
 * Also, puts leading zeroes when necessary.
 *
 * @param hexString The hex string to be formatted.
 * @param groupLen  The length of the groups of nibbles.
 * @return The formatted hex string.
 */
public static String format(final String hexString, final int groupLen) {
    final String unformatted = unformat(hexString);
    ArgumentUtils.rejectIfDontMatches(unformatted, HEX_PATTERN);
    final StringBuilder buffer = new StringBuilder();
    final StringBuilder formatted = new StringBuilder();
    for (int i = CharSequenceUtils.indexOfLastChar(unformatted); i >= 0; i--) {
        buffer.insert(0, unformatted.charAt(i));
        if (buffer.length() == groupLen) {
            /* When the buffer reaches 'groupLen' size, its contents is passed to 'formatted'. */
            if (i > 0) {
                /*
                 * If unprocessed chars remains on the original string, them we put the separator on the buffer
                 * start.
                 */
                buffer.insert(0, GROUP_SEPARATOR);
            }
            /* we pass the buffer value to the 'formatted' accumulator */
            formatted.insert(0, buffer);
            /* empty the buffer */
            buffer.replace(0, buffer.length(), "");
        }
    }
    /* If unprocessed chars remains on the buffer, it means that we need to fill it up with trailing zeroes. */
    if (buffer.length() > 0) {
        buffer.insert(0, StringUtils.repeat("0", groupLen - buffer.length()));
        /* we pass the buffer value to the 'formatted' accumulator */
        formatted.insert(0, buffer);
    }
    return formatted.toString();
}

From source file:com.lightbox.android.webservices.requests.ApiRequest.java

private static String insertParametersInPath(String url, Map<String, Object> parameters) {
    StringBuilder urlStringBuilder = new StringBuilder(url);
    if (parameters != null) {
        for (Iterator<Entry<String, Object>> iterator = parameters.entrySet().iterator(); iterator.hasNext();) {
            Entry<String, Object> paramEntry = iterator.next();
            String pathParamName = String.format("{%s}", paramEntry.getKey());
            int startIndex = urlStringBuilder.indexOf(pathParamName);
            if (startIndex != -1) {
                // We found the parameter name in the path: replace it, and remove it from the parameter map
                int endIndex = startIndex + pathParamName.length();
                urlStringBuilder.replace(startIndex, endIndex, paramEntry.getValue().toString());
                iterator.remove();/*from   w  w  w. ja v  a 2  s. com*/
            }
        }
    }
    return urlStringBuilder.toString();
}

From source file:org.onosproject.t3.cli.T3CliUtils.java

private static String printTreatment(TrafficTreatment treatment) {
    final String delimiter = ", ";
    StringBuilder builder = new StringBuilder("[");
    if (!treatment.immediate().isEmpty()) {
        builder.append("immediate=" + treatment.immediate() + delimiter);
    }//  w  w  w .j a  va 2  s  .  c  o m
    if (!treatment.deferred().isEmpty()) {
        builder.append("deferred=" + treatment.deferred() + delimiter);
    }
    if (treatment.clearedDeferred()) {
        builder.append("clearDeferred" + delimiter);
    }
    if (treatment.tableTransition() != null) {
        builder.append("transition=" + treatment.tableTransition() + delimiter);
    }
    if (treatment.metered() != null) {
        builder.append("meter=" + treatment.metered() + delimiter);
    }
    if (treatment.writeMetadata() != null) {
        builder.append("metadata=" + treatment.writeMetadata() + delimiter);
    }
    // Chop off last delimiter
    builder.replace(builder.length() - delimiter.length(), builder.length(), "");
    builder.append("]");
    return builder.toString();
}

From source file:pl.nask.hsn2.normalizers.URLNormalizerUtils.java

public static String removeObfuscatedEncoding(StringBuilder sb, int startInd, int endInd, EncodingType[] type) {
    StringBuilder nsb = new StringBuilder(sb.subSequence(startInd, endInd));
    removeObfuscatedEncoding(nsb, type);
    sb.replace(startInd, endInd, nsb.toString());
    return nsb.toString();
}

From source file:com.aurel.track.exchange.docx.exporter.AssembleWordprocessingMLPackage.java

private static StringBuilder replaceIssueLinksWithDescription(StringBuilder src, List<Integer> itemIDs) {
    StringBuilder lsrc = new StringBuilder(src);
    int startIndex = 0;
    String paragraph = "<p>";
    while ((startIndex = lsrc.toString().indexOf(ISSUE_TAG, startIndex)) != -1) {
        int endIndex = lsrc.toString().indexOf(CLOSE, startIndex);
        if (endIndex == -1 || (endIndex <= startIndex)) {
            lsrc = lsrc.replace(startIndex, startIndex + ISSUE_TAG.length(), EMPTY);
        } else {/*from   w  w  w . j  a  v a  2s.  c o  m*/
            String key = lsrc.substring(startIndex + ISSUE_TAG.length(), endIndex).trim();
            try {
                Integer itemID = Integer.decode(key);
                LOGGER.debug("ItemID " + itemID + " found");
                itemIDs.add(itemID);
                TWorkItemBean itemBean = null;
                try {
                    itemBean = ItemBL.loadWorkItem(itemID);
                } catch (ItemLoaderException e) {
                    LOGGER.warn("Loading the workItemID " + itemID + " failed with " + e.getMessage());
                }
                if (itemBean == null) {
                    lsrc = lsrc.replace(startIndex, endIndex + CLOSE.length(), EMPTY);
                } else {
                    String description = itemBean.getDescription();
                    if (description == null || description.length() == 0) {
                        description = itemBean.getSynopsis();
                    } else {
                        description = exportDescription(description, itemIDs);
                    }
                    if (description == null || description.length() == 0) {
                        break;
                    }
                    //add itemNo before the inline description
                    if (description.startsWith(paragraph)) {
                        description = paragraph + AssembleWordprocessingMLPackage.getItemNo(itemBean)
                                + description.substring(paragraph.length());
                    } else {
                        description = AssembleWordprocessingMLPackage.getItemNo(itemBean) + description;
                    }
                    lsrc = lsrc.replace(startIndex, endIndex + CLOSE.length(), description);
                    startIndex = startIndex + description.length();

                }
            } catch (NumberFormatException e) {
                lsrc = lsrc.replace(startIndex, startIndex + ISSUE_TAG.length(), EMPTY);
                //recalculate end index
                endIndex = lsrc.toString().indexOf(CLOSE, startIndex);
                lsrc = lsrc.replace(endIndex, endIndex + CLOSE.length(), EMPTY);
            }
        }
    }
    return lsrc;
}

From source file:org.openspaces.pu.container.support.BeanLevelPropertiesUtils.java

public static String resolvePlaceholders(String text, BeanLevelProperties beanLevelProperties) {
    StringBuilder buf = new StringBuilder(text);

    int startIndex = buf.indexOf(PLACEHOLDER_PREFIX);
    while (startIndex != -1) {
        int endIndex = buf.indexOf(PLACEHOLDER_SUFFIX, startIndex + PLACEHOLDER_PREFIX.length());
        if (endIndex != -1) {
            String placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex);
            int nextIndex = endIndex + PLACEHOLDER_SUFFIX.length();
            try {
                String propVal = beanLevelProperties.getContextProperties().getProperty(placeholder);
                if (propVal == null) {
                    propVal = System.getProperty(placeholder);
                    if (propVal == null) {
                        // Fall back to searching the system environment.
                        propVal = System.getenv(placeholder);
                    }/*from  w ww .ja v  a  2 s . co m*/
                }
                if (propVal != null) {
                    buf.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), propVal);
                    nextIndex = startIndex + propVal.length();
                }
            } catch (Throwable ex) {
                if (logger.isWarnEnabled()) {
                    logger.warn("Could not resolve placeholder '" + placeholder + "' in [" + text
                            + "] as system property: " + ex);
                }
            }
            startIndex = buf.indexOf(PLACEHOLDER_PREFIX, nextIndex);
        } else {
            startIndex = -1;
        }
    }

    return buf.toString();
}