Java Map Replace replaceString(String source, Map args)

Here you can find the source of replaceString(String source, Map args)

Description

Replace string

License

Open Source License

Declaration

public static String replaceString(String source, Map args) 

Method Source Code

//package com.java2s;
/***************************************************************************
 * Copyright (c) 2006 Eike Stepper, Fuggerstr. 39, 10777 Berlin, Germany.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * //from  w ww  . jav a2s . c o  m
 * Contributors:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/

import java.util.Map;

public class Main {
    /**
     * Replace string
     */
    public static String replaceString(String source, Map args) {
        int startIndex = 0;
        int openIndex = source.indexOf('{', startIndex);
        if (openIndex == -1) {
            return source;
        }

        int closeIndex = source.indexOf('}', startIndex);
        if ((closeIndex == -1) || (openIndex > closeIndex)) {
            return source;
        }

        StringBuffer sb = new StringBuffer();
        sb.append(source.substring(startIndex, openIndex));
        while (true) {
            String key = source.substring(openIndex + 1, closeIndex);
            Object val = args.get(key);
            if (val != null) {
                sb.append(val);
            }

            startIndex = closeIndex + 1;
            openIndex = source.indexOf('{', startIndex);
            if (openIndex == -1) {
                sb.append(source.substring(startIndex));
                break;
            }

            closeIndex = source.indexOf('}', startIndex);
            if ((closeIndex == -1) || (openIndex > closeIndex)) {
                sb.append(source.substring(startIndex));
                break;
            }
            sb.append(source.substring(startIndex, openIndex));
        }
        return sb.toString();
    }

    /**
     * Replace string
     */
    public static String replaceString(String source, Object[] args) {
        int startIndex = 0;
        int openIndex = source.indexOf('{', startIndex);
        if (openIndex == -1) {
            return source;
        }

        int closeIndex = source.indexOf('}', startIndex);
        if ((closeIndex == -1) || (openIndex > closeIndex)) {
            return source;
        }

        StringBuffer sb = new StringBuffer();
        sb.append(source.substring(startIndex, openIndex));
        while (true) {
            String intStr = source.substring(openIndex + 1, closeIndex);
            int index = Integer.parseInt(intStr);
            sb.append(args[index]);

            startIndex = closeIndex + 1;
            openIndex = source.indexOf('{', startIndex);
            if (openIndex == -1) {
                sb.append(source.substring(startIndex));
                break;
            }

            closeIndex = source.indexOf('}', startIndex);
            if ((closeIndex == -1) || (openIndex > closeIndex)) {
                sb.append(source.substring(startIndex));
                break;
            }
            sb.append(source.substring(startIndex, openIndex));
        }
        return sb.toString();
    }

    /**
     * This is a string replacement method.
     */
    public static String replaceString(String source, String oldStr, String newStr) {
        StringBuffer sb = new StringBuffer(source.length());
        int sind = 0;
        int cind = 0;
        while ((cind = source.indexOf(oldStr, sind)) != -1) {
            sb.append(source.substring(sind, cind));
            sb.append(newStr);
            sind = cind + oldStr.length();
        }
        sb.append(source.substring(sind));
        return sb.toString();
    }
}

Related

  1. replacePlaceholders(String string, Map values)
  2. replaceProperties(final CharSequence string, final Map properties)
  3. replaceQueryParameterInUrl(String fullUrl, Map newParameter)
  4. replaceSeparator(String value, String separator, Map map)
  5. replaceString(Map bodyToReplace, String replacingContent)
  6. replaceString(String uri, String baseURI, Map prefixes)
  7. replaceTemplates(String template, Map entries)
  8. replaceTokens(String inputString, Map tokenMap)
  9. replaceToStringBuilder(String s, String begin, String end, Map values)