Java Map Replace replaceValues(String s, String begin, String end, Map values)

Here you can find the source of replaceValues(String s, String begin, String end, Map values)

Description

Returns a string with replaced values.

License

Open Source License

Parameter

Parameter Description
s the original string
begin the beginning delimiter
end the ending delimiter
values a map of old and new values

Return

a string with replaced values

Declaration

public static String replaceValues(String s, String begin, String end, Map values) 

Method Source Code

//package com.java2s;
/**/*  w w w  . j  a v  a 2s.  c  om*/
  * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
  * in the Software without restriction, including without limitation the rights
  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  * copies of the Software, and to permit persons to whom the Software is
  * furnished to do so, subject to the following conditions:
  *
  * The above copyright notice and this permission notice shall be included in
  * all copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */

import java.util.Map;

public class Main {
    /**
      * Returns a string with replaced values. This method will replace all text
      * in the given string, between the beginning and ending delimiter, with new
      * values found in the given map. For example, if the string contained the
      * text <code>[$HELLO$]</code>, and the beginning delimiter was
      * <code>[$]</code>, and the ending delimiter was <code>$]</code>, and
      * the values map had a key of <code>HELLO</code> that mapped to
      * <code>WORLD</code>, then the replaced string will contain the text
      * <code>[$WORLD$]</code>.
      * 
      * @param s
      *            the original string
      * @param begin
      *            the beginning delimiter
      * @param end
      *            the ending delimiter
      * @param values
      *            a map of old and new values
      * @return a string with replaced values
      */
    public static String replaceValues(String s, String begin, String end, Map values) {

        if ((s == null) || (begin == null) || (end == null) || (values == null) || (values.size() == 0)) {

            return s;
        }

        StringBuffer sm = new StringBuffer(s.length());

        int pos = 0;

        while (true) {
            int x = s.indexOf(begin, pos);
            int y = s.indexOf(end, x + begin.length());

            if ((x == -1) || (y == -1)) {
                sm.append(s.substring(pos, s.length()));

                break;
            } else {
                sm.append(s.substring(pos, x + begin.length()));

                String oldValue = s.substring(x + begin.length(), y);

                String newValue = (String) values.get(oldValue);

                if (newValue == null) {
                    newValue = oldValue;
                }

                sm.append(newValue);

                pos = y;
            }
        }

        return sm.toString();
    }
}

Related

  1. replaceString(String source, Map args)
  2. replaceString(String uri, String baseURI, Map prefixes)
  3. replaceTemplates(String template, Map entries)
  4. replaceTokens(String inputString, Map tokenMap)
  5. replaceToStringBuilder(String s, String begin, String end, Map values)
  6. replaceValuesToSynonyms(Map dic, Map synonims)
  7. replaceVariable(final String src, final Map value)
  8. replaceVariables(String chart, Map variables)
  9. replaceVars(String origString, Map vars)