Java Map Replace replaceAllRegex(String input, Map replacements)

Here you can find the source of replaceAllRegex(String input, Map replacements)

Description

replace All Regex

License

Apache License

Declaration

public static String replaceAllRegex(String input, Map<String, String> replacements) 

Method Source Code

//package com.java2s;
/*/*from   ww  w.j av  a  2 s. c  o  m*/
 * $Id: StringUtil.java,v 1.2 2009-06-30 14:14:38 concentus Exp $
 * 
 * Copyright 2005 Sebastian Hasait
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Iterator;

import java.util.Map;
import java.util.Map.Entry;

public class Main {
    public static String replaceAllRegex(String input, Map<String, String> replacements) {
        if (input == null) {
            return null;
        }
        Iterator<Entry<String, String>> replacementsI = replacements.entrySet().iterator();
        Entry<String, String> replacement;
        String current = input;
        while (replacementsI.hasNext()) {
            replacement = replacementsI.next();
            current = current.replaceAll(replacement.getKey(), replacement.getValue());
        }
        return current;
    }

    public static String replaceAll(final String input, final Map<String, String> replacements) {
        if (input == null) {
            return null;
        }
        Iterator<Entry<String, String>> replacementsI = replacements.entrySet().iterator();
        Entry<String, String> replacement;
        String current = input;
        while (replacementsI.hasNext()) {
            replacement = replacementsI.next();
            current = replace(current, replacement.getKey(), replacement.getValue());
        }
        return current;
    }

    public static String replace(final String input, final String search, final String replace) {
        if (input == null) {
            return null;
        }
        int index = input.lastIndexOf(search);
        if (index >= 0) {
            StringBuffer result = new StringBuffer(input);
            result.replace(index, index + search.length(), replace);
            return replace(result.toString(), search, replace);
        }
        return input;
    }

    public static String toString(final Object object, final String nullString) {
        if (object == null) {
            return nullString;
        }
        return object.toString();
    }

    public static String toString(final Object object) {
        return toString(object, null);
    }
}

Related

  1. replace2(String str, Map data)
  2. replaceAll(Map globals, String value)
  3. replaceAll(String inTemplate, Map inVars)
  4. replaceAll(String template, Map variables)
  5. replaceAllOccurrences(String str, Map replacementMap)
  6. replaceByMap(String _searchStr, Map _replacements)
  7. replaceByMap(String tpl, Map map)
  8. replaceCharacters(String sequence, Map map, boolean reverse)
  9. replaceCharactersInArray(char[] characters, Map replacements)