Example usage for org.apache.commons.lang3 StringUtils replace

List of usage examples for org.apache.commons.lang3 StringUtils replace

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils replace.

Prototype

public static String replace(final String text, final String searchString, final String replacement) 

Source Link

Document

Replaces all occurrences of a String within another String.

A null reference passed to this method is a no-op.

 StringUtils.replace(null, *, *)        = null StringUtils.replace("", *, *)          = "" StringUtils.replace("any", null, *)    = "any" StringUtils.replace("any", *, null)    = "any" StringUtils.replace("any", "", *)      = "any" StringUtils.replace("aba", "a", null)  = "aba" StringUtils.replace("aba", "a", "")    = "b" StringUtils.replace("aba", "a", "z")   = "zbz" 

Usage

From source file:io.fabric8.maven.enricher.fabric8.DocLinkEnricher.java

/**
 * Replaces all text of the form <code>${foo}</code> with the value in the properties object
 *//*from w  ww.j  av  a2s  .  c o m*/
protected static String replaceProperties(String text, Properties properties) {
    Set<Map.Entry<Object, Object>> entries = properties.entrySet();
    for (Map.Entry<Object, Object> entry : entries) {
        Object key = entry.getKey();
        Object value = entry.getValue();
        if (key != null && value != null) {
            String pattern = "${" + key + "}";
            text = StringUtils.replace(text, pattern, value.toString());
        }
    }
    return text;
}

From source file:info.magnolia.ui.form.field.transformer.multi.MultiValueJSONTransformer.java

/**
 * Simple utility method that remove the , for every elements of a String List.
 *///from  w  w  w  .  j  a v a  2  s  .  c  o  m
private List<String> removeComma(PropertysetItem newValue) {
    List<String> res = new ArrayList<String>();
    if (newValue != null) {
        Iterator<?> it = newValue.getItemPropertyIds().iterator();
        while (it.hasNext()) {
            String element = newValue.getItemProperty(it.next()).getValue().toString();
            res.add(StringUtils.replace(element, ",", " "));
        }
    }
    return res;
}

From source file:com.codspire.mojo.artifactlookup.ArtifactLookupMojoTest.java

private String replace(String text, String searchString, String replacement) {
    return StringUtils.replace(text, searchString, replacement);
}

From source file:com.conversantmedia.mapreduce.tool.ExpressionEvaluator.java

/**
 *
 * @param rootObj         root object for the OGNL context
 * @param context         set of properties fed into evaluation
 * @param expr            the expression to evaluate. Expects an OGNL expression.
 * @return               the result of the expression's evaluation
 * @throws ToolException   if something goes wrong
 *//*from ww  w .  j a  v a  2s  .  co  m*/
@SuppressWarnings("rawtypes")
public Object evaluate(Object rootObj, Map<String, Object> context, String expr) throws ToolException {

    Object value = expr;
    Object expressionValue = null;

    Map ognlContext = buildOgnlContext(rootObj, context);

    try {
        Matcher matcher = pattern.matcher(expr);
        while (matcher.matches()) {
            expr = matcher.group(2); // remove the enclosing ${ and }

            // If it doesn't start with 'context.' or 'this.' then prepend the
            // expression with 'this.'
            if (!(StringUtils.startsWith(expr, OgnlContext.THIS_CONTEXT_KEY + ".")
                    || StringUtils.startsWith(expr, "context."))) {
                expr = "this." + expr;
            } else {
                expr = StringUtils.replace(expr, "context", TOOL_CONTEXT_KEY);
            }

            expressionValue = Ognl.getValue(expr, ognlContext);
            if (StringUtils.isNotBlank(matcher.group(1)) || StringUtils.isNotBlank(matcher.group(3))) {
                value = matcher.group(1) + expressionValue + matcher.group(3);
            } else {
                value = expressionValue;
            }
            matcher = pattern.matcher(value.toString());
        }
    } catch (OgnlException e) {
        throw new ToolException("Failed to evaluate [" + expr + "]", e);
    }
    return value;
}

From source file:com.rodaxsoft.mailgun.CampaignManager.java

/**
 * Saves campaign events to a CSV file with the following format:
 * <code>&lt;campaign name&gt;_(&lt;campaign id&gt;)_&lt;timestamp&gt;.csv</code>
 * @param campaignId The campaign ID// w  ww. j  av  a 2s .  c  o  m
 * @throws ContextedException if a processing error occurs
 * @throws IOException if an I/O error occurs
 */
void saveCampaignEventsToCSV(String campaignId) throws ContextedException, IOException {

    Campaign campaign = getCampaign(campaignId);

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

    String dateTime = format.format(new Date());
    String fileName;

    if (campaign != null) {

        String name = StringUtils.replace(campaign.getName(), " ", "_");
        fileName = new StringBuilder(name).append("_(").append(campaignId).append(")_").append(dateTime)
                .append(".csv").toString();
    } else {
        fileName = campaignId + "_" + dateTime + ".csv";
    }

    CSVPrinter csvPrinter = null;
    PrintWriter pw = null;
    CSVFormat csvFormat = null;
    try {

        pw = new PrintWriter(fileName);
        final List<Map<String, Object>> events = getEvents(campaignId);

        for (Map<String, Object> map : events) {

            if (null == csvPrinter) {
                final Set<String> keySet = map.keySet();
                int size = keySet.size();
                String[] keys = keySet.toArray(new String[size]);
                csvFormat = CSVFormat.DEFAULT.withHeader(keys);
                csvPrinter = new CSVPrinter(pw, csvFormat);
            }
            //            city   domain   tags   timestamp   region   ip   country   recipient   event   user_vars

            String[] headers = csvFormat.getHeader();
            for (String key : headers) {
                csvPrinter.print(map.get(key));
            }

            csvPrinter.println();
        }

    } finally {

        if (csvPrinter != null) {
            csvPrinter.flush();
        }

        IOUtils.closeQuietly(csvPrinter);
    }

}

From source file:com.netsteadfast.greenstep.util.ScriptExpressionUtils.java

public static String replaceFormulaExpression(String type, String expression) throws Exception {
    if (StringUtils.isBlank(expression)) {
        return expression;
    }// w w w .  j  a  v a2 s. c  o m
    String bscExpression = expression;
    bscExpression = StringUtils.replace(bscExpression, "", "/");
    bscExpression = StringUtils.replace(bscExpression, "", "*");
    bscExpression = StringUtils.replace(bscExpression, "", "-");
    bscExpression = StringUtils.replace(bscExpression, "+", "+");
    if (!ScriptTypeCode.IS_PYTHON.equals(type)) {
        bscExpression = StringUtils.replace(bscExpression, "abs(", "Math.abs(");
        bscExpression = StringUtils.replace(bscExpression, "sqrt(", "Math.sqrt(");
    }
    return bscExpression;
}

From source file:com.glaf.core.util.RequestUtils.java

public static String decodeString(String str) {
    try {// w ww . ja va 2 s  .  c o  m
        // Base64 base64 = new Base64();
        // byte[] bytes = base64.decode(StringTools.decodeHex(str));
        // String tmp = new String(bytes);
        String tmp = str;
        Encryptor cryptor = EncryptorFactory.getEncryptor();
        tmp = cryptor.decrypt(tmp);
        String salt = DigestUtils.md5Hex(SystemConfig.getToken());
        tmp = StringUtils.replace(tmp, salt, "");
        return tmp;
    } catch (Exception ex) {
        return str;
    }
}

From source file:glluch.com.ontotaxoseeker.TaxoPath.java

/**
 * Given a list of terms (with counts) builds a list of path, with counts. So, 
 * transforms each term into null if it isn't in the taxonomy or in its path if 
 * it is in.//from w  ww . j ava 2  s. c  o  m
 * @param terms The list of terms and its counts.
 * @return A list of paths from the taxonomy and its counts.
 * @throws IOException Reading a file in freeling.
 */
protected PathsCount findPaths(TermsCount terms) throws IOException {
    PathsCount termsPaths = new PathsCount();
    Iterator termsIte = terms.iterator();
    //debug("PathsCount findPaths");
    while (termsIte.hasNext()) {
        String key = (String) termsIte.next();
        String termUri = StringUtils.replace(key, " ", "_");
        termUri = Config.NS + termUri;
        Path termPath = this.path(termUri);
        //debug("termUri is "+termUri);
        if (termPath != null) {
            termsPaths.put(termPath, terms.get(key));
            //debug("\ttermPath is "+termPath.prettyPrint());
        } else {
            //debug("\tthere isn't in IEEE taxonomy");
        }
    }
    return termsPaths;

}

From source file:com.epam.dlab.core.parser.CommonFormat.java

/**
 * Add the column value to string in CSV format.
 *
 * @param sb    the buffer of sting./*  w ww  .j a  v  a  2 s. co m*/
 * @param value the value.
 * @return the buffer of string.
 */
private static StringBuilder addToStringBuilder(StringBuilder sb, String value) {
    if (sb.length() > 0) {
        sb.append(FIELD_SEPARATOR);
    }
    return sb.append(FIELD_DELIMITER)
            .append(StringUtils.replace(value, DELIMITER_REPLACE_FROM, DELIMITER_REPLACE_TO))
            .append(FIELD_DELIMITER);
}

From source file:net.ontopia.topicmaps.nav2.portlets.pojos.Wiki.java

private static String makeList(String rawlist) {
    return "<ul>\n" + StringUtils.replace(rawlist, "*", "<li>") + "</ul>";
}