Java Map Replace replace(String src, String prefix, String suffix, Map props)

Here you can find the source of replace(String src, String prefix, String suffix, Map props)

Description

replace a symbol in a string;

License

Open Source License

Parameter

Parameter Description
src a parameter
prefix a parameter
suffix a parameter
props a parameter

Declaration

public final static String replace(String src, String prefix, String suffix, Map props) 

Method Source Code

//package com.java2s;

import java.util.Map;

public class Main {
    /**// w w w  .  ja va2  s.  c om
     * replace a symbol in a string;
     *
     * @param src
     * @param prefix
     * @param suffix
     * @param props
     * @return
     */
    public final static String replace(String src, String prefix, String suffix, Map props) {
        int index1;
        int index2;
        int len1 = prefix.length();
        int len2 = suffix.length();

        StringBuffer sb = new StringBuffer();

        index1 = src.indexOf(prefix);
        while (index1 >= 0) {
            sb.append(src.substring(0, index1));
            src = src.substring(index1 + len1);
            if (src.startsWith(prefix)) {
                sb.append(prefix);
                break;
            } else {
                index2 = src.indexOf(suffix);
                if (index2 >= 0) {
                    String t = src.substring(0, index2);
                    Object o = props.get(t);
                    String sp = (o == null ? "" : o.toString());
                    sb.append(sp);
                    src = src.substring((index2 + len2));
                    index1 = src.indexOf(prefix);
                } else {
                    sb.append(prefix);
                    break;
                }
            }
        }
        sb.append(src);
        return new String(sb);
    }
}

Related

  1. replace(Map map, String content)
  2. replace(Map map, String text)
  3. replace(String originalCommand, Map vars)
  4. replace(String orign, Map replaceStringMap)
  5. replace(String s, Map map)
  6. replace(String str, Map values)
  7. replace(String string, Map values)
  8. replace(String target, Map arguments)
  9. replace(String temp, Map tags)