Java Map Replace replace(String text, Map pairs)

Here you can find the source of replace(String text, Map pairs)

Description

replace

License

Open Source License

Declaration

public static String replace(String text, Map<String, ?> pairs) 

Method Source Code

//package com.java2s;
/*//  ww  w  .  j  a  v  a2s .  c om
 * Copyright (C) 2013 DASISH
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.util.Map;

public class Main {
    public static String replace(String text, Map<String, ?> pairs) {
        StringBuilder result = new StringBuilder(text);
        for (String old : pairs.keySet()) {
            if (old != null) {
                if (!old.equals("")) {
                    replaceString(result, old, pairs.get(old));
                }
            }
        }
        return result.toString();
    }

    public static StringBuilder replaceString(StringBuilder source,
            String oldFragment, Object newObject) {
        if (oldFragment != null) {
            int lengthOld = oldFragment.length();
            String newFragment;
            if (newObject != null) {
                if (newObject instanceof Integer) {
                    newFragment = ((Integer) newObject).toString();
                } else {
                    if (newObject instanceof String) {
                        newFragment = (String) newObject;
                    } else {
                        newFragment = newObject.toString();
                    }
                }
            } else {
                newFragment = " ";
            }
            int lengthNew = newFragment.length();
            int indexOf = source.indexOf(oldFragment);
            while (indexOf > 0) {
                source.delete(indexOf, indexOf + lengthOld);
                source.insert(indexOf, newFragment);
                indexOf = source.indexOf(oldFragment, indexOf + lengthNew);
            }
        }
        return source;
    }
}

Related

  1. replace(String str, Map values)
  2. replace(String string, Map values)
  3. replace(String target, Map arguments)
  4. replace(String temp, Map tags)
  5. replace(String template, Map funcAndDic)
  6. replace2(String str, Map data)
  7. replaceAll(Map globals, String value)
  8. replaceAll(String inTemplate, Map inVars)
  9. replaceAll(String template, Map variables)