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.codecentric.boot.admin.web.servlet.resource.ConcatenatingResourceResolver.java

private String buildDescription(Collection<? extends Resource> resources) {
    StringBuilder sb = new StringBuilder("(");
    for (Resource resource : resources) {
        sb.append(resource.getDescription()).append(", ");
    }/*from  ww w.j  ava2s  .  c o  m*/
    sb.replace(sb.length() - 2, sb.length(), ")");
    return sb.toString();
}

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

/**
 * @param sb/*from   w w  w. jav  a 2s .  c om*/
 * @return
 * @throws URLHostParseException
 */
public static String decodeIPv4(StringBuilder sb, int startIndx, int endIndx) throws URLHostParseException {
    long[] ip = decodeIPv4(sb.substring(startIndx, endIndx));
    StringBuilder str = new StringBuilder();
    for (int i = 0; i < ip.length; i++) {
        str.append(ip[i]);
        if (i < ip.length - 1) {
            str.append(".");
        }
    }
    sb.replace(startIndx, endIndx, str.toString());
    return str.toString();

}

From source file:nz.net.orcon.kanban.automation.plugin.TemplatePlugin.java

private void replaceAll(String field, String value, StringBuilder builder) {

    int x = 0;//from   w w w . jav  a  2s .  co  m
    String toFind = "${" + field + "}";
    while (x >= 0) {
        x = builder.indexOf(toFind);
        if (x >= 0) {
            builder.replace(x, x + toFind.length(), value);
        }
    }
}

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

public static String normalizeUserInfo(StringBuilder in, int start, int end) throws URIException {
    int endSq = URLNormalizerUtils.findFirstMatch(in, "@", start, end);
    if (endSq < 0) {
        endSq = end;/*from   ww w .j av  a 2s  .  co m*/
    }
    StringBuilder sb = new StringBuilder(in.substring(start, endSq));

    int sep = findFirstMatch(sb, ":", 0);
    String enc = null;
    if (sep > 0) {
        removeObfuscatedEncoding(sb, 0, sep, new EncodingType[] { EncodingType.USERINFO });
        sep = findFirstMatch(sb, ":", 0);
        enc = URIUtil.encodeWithinAuthority(sb.substring(0, sep));
        sb.replace(0, sep, enc);
    } else {
        removeObfuscatedEncoding(sb, new EncodingType[] { EncodingType.USERINFO });
        enc = URIUtil.encodeWithinAuthority(sb.toString());
        sb.replace(0, sb.length(), enc);
    }
    in.replace(start, end, sb.toString());
    return sb.toString();
}

From source file:gaffer.example.util.Example.java

protected String getMethodNameAsSentence(final int parentMethod) {
    final String[] words = getMethodName(parentMethod + 1).split(CAPITALS_AND_NUMBERS_REGEX);
    final StringBuilder sentence = new StringBuilder();
    for (final String word : words) {
        sentence.append(word.toLowerCase(Locale.getDefault())).append(" ");
    }//ww w  .j  a  v a 2s.  c o m
    sentence.replace(0, 1, sentence.substring(0, 1).toUpperCase(Locale.getDefault()));
    sentence.replace(sentence.length() - 1, sentence.length(), "");
    return sentence.toString();
}

From source file:dhr.uploadtomicrobit.FirmwareGenerator.java

public void replaceAll(StringBuilder builder, String from, String to) {
    int index = builder.indexOf(from);
    while (index != -1) {
        builder.replace(index, index + from.length(), to);
        index += to.length(); // Move to the end of the replacement
        index = builder.indexOf(from, index);
    }//  w ww. j a va 2 s .  c  om
}

From source file:org.cyclop.common.StringHelper.java

public static String decorate(String toDecorate, StringDecorator decorator, String... keywordsLc) {
    String prefix = decorator.prefix();
    String postfix = decorator.postfix();

    int prefixLength = prefix.length();
    StringBuilder buf = new StringBuilder(toDecorate);
    for (String keyword : sortBySize(keywordsLc)) {
        if (prefix.contains(keyword) || postfix.contains(keyword)) {
            LOG.debug("Skipping keyword: {} because it's part of decoreator syntax", keyword);
            continue;
        }//from w w w  . j  a  va2  s  .com
        String bufLc = buf.toString().toLowerCase();
        int startSearch = 0;
        int foundIdx = -1;
        while ((foundIdx = bufLc.indexOf(keyword, startSearch)) >= 0) {
            if (foundIdx > prefixLength) {
                String kwWithPref = bufLc.substring(foundIdx - prefixLength, foundIdx);
                if (prefix.equals(kwWithPref)) {
                    startSearch = foundIdx + kwWithPref.length() + keyword.length() + 1;
                    // word already replaced with longer one (better match)
                    continue;
                }
            }
            int kwStart = foundIdx;
            int kwEnd = foundIdx + keyword.length();
            String orgKw = buf.substring(kwStart, kwEnd);
            String decoratedKw = decorator.decorate(orgKw);
            buf.replace(kwStart, kwEnd, decoratedKw);

            bufLc = buf.toString().toLowerCase();
            startSearch = foundIdx + decoratedKw.length() + 1;
        }
    }
    LOG.trace("Decorated {} to {}", toDecorate, buf);
    return buf.toString();
}

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

public static String normalizeQuery(StringBuilder sb, int startIndx, int endIndx)
        throws URLMalformedInputException {
    if (sb.indexOf("?") != 0) {
        throw new URLMalformedInputException("Query should start with '?'");
    }/* w  ww  . java2 s.  com*/
    int end = findFirstMatch(sb, "#", startIndx);
    if (end < 0 || end > endIndx) {
        end = endIndx;
    }
    StringBuilder enc = new StringBuilder(sb.substring(startIndx, end));
    removeObfuscatedEncoding(enc,
            new EncodingType[] { EncodingType.PLUS_AS_SPACE, EncodingType.QUERY_ALLOWED });
    int i = findFirstMatch(enc, "%", 0);
    try {
        if (i < 0) {
            String tmp = URIUtil.encodeQuery(enc.toString(), DEFAULT_CHARSET);
            enc.replace(0, enc.length(), tmp);
        } else {
            int start = 0;
            String tmp = "";
            while ((i = findFirstMatch(enc, "%", start)) > 0) {
                if (percentEnc.matcher(enc.substring(i)).matches()) {
                    tmp = URIUtil.encodeQuery(enc.substring(start, i), DEFAULT_CHARSET);
                    enc.replace(start, i, tmp);
                    start = i + 1 - (tmp.length() - (i - start));
                } else {
                    enc.replace(i, i + 1, "%25");
                    start = i + 1;
                }
            }
            tmp = URIUtil.encodeQuery(enc.substring(start), DEFAULT_CHARSET);
            enc.replace(start, enc.length(), tmp);
        }
    } catch (URIException e) {
        throw new URLMalformedInputException(e);
    }
    i = 0;
    while ((i = findFirstMatch(enc, ";", i)) > 0) {
        enc.replace(i, i + 1, "&");
    }
    i = 0;
    while ((i = findFirstMatch(enc, "+ ", i)) > 0) {
        enc.replace(i, i + 1, "%20");
    }
    sb.replace(startIndx, end, enc.toString());
    return enc.toString();

}

From source file:org.apache.ambari.server.checks.AbstractCheckDescriptor.java

/**
 * Formats lists of given entities to human readable form:
 * [entity1] -> {entity1} {noun}//from  ww w .  j av  a 2  s.c o  m
 * [entity1, entity2] -> {entity1} and {entity2} {noun}s
 * [entity1, entity2, entity3] -> {entity1}, {entity2} and {entity3} {noun}s
 * The noun for the entities is taken from check type, it may be cluster, service or host.
 *
 * @param entities list of entities to format
 * @return formatted entity list
 */
protected String formatEntityList(List<String> entities) {
    if (entities == null || entities.isEmpty()) {
        return "";
    }
    final StringBuilder formatted = new StringBuilder(StringUtils.join(entities, ", "));
    if (entities.size() > 1) {
        formatted.replace(formatted.lastIndexOf(","), formatted.lastIndexOf(",") + 1, " and");
    }

    return formatted.toString();
}

From source file:org.jenkinsmvn.jenkins.mvn.plugin.handler.HandlerUtils.java

public String getReplacement(String orig, Action action) {
    String replacement = getRequiredProperty(action, REPLACEMENT);

    // direct replacement
    if (!isPropertyExist(action, REGEX_REPLACE_PATTERN)) {
        return replacement;
    }/*from   w w w. j  a  v a2  s. c om*/

    String regexReplacePattern = getRequiredProperty(action, REGEX_REPLACE_PATTERN);
    int group = -1;

    if (isPropertyExist(action, REGEX_REPLACE_PATTERN_GROUP)) {
        try {
            group = Integer.parseInt(getRequiredProperty(action, REGEX_REPLACE_PATTERN_GROUP));
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException(String.format("Property '%s' for action '%s' is not a integer.",
                    REGEX_REPLACE_PATTERN_GROUP, action.getName()));
        }
    }

    Pattern pattern = Pattern.compile(regexReplacePattern);
    Matcher matcher = pattern.matcher(orig);

    if (!matcher.find()) {
        throw new IllegalArgumentException(String.format(
                "Unable to replace find replacement for '%s' with the given pattern and group on action '%s'.",
                orig, action.getName()));
    }

    if (group == -1) {
        return orig.replaceFirst(regexReplacePattern, replacement);
    }

    int start = matcher.start(group);
    int end = matcher.end(group);

    StringBuilder buf = new StringBuilder(orig);
    buf.replace(start, end, replacement);

    return buf.toString();
}