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

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

Description

replace To String Builder

License

Open Source License

Declaration

public static StringBuilder replaceToStringBuilder(String s, String begin, String end,
            Map<String, String> values) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Map;

public class Main {
    public static StringBuilder replaceToStringBuilder(String s, String begin, String end,
            Map<String, String> values) {

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

            return new StringBuilder(s);
        }/*from  w  ww. jav a2  s  .c om*/

        StringBuilder sb = new StringBuilder(values.size() * 2 + 1);

        int pos = 0;

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

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

                break;
            } else {
                sb.append(s.substring(pos, x));

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

                String newValue = values.get(oldValue);

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

                sb.append(newValue);

                pos = y + end.length();
            }
        }

        return sb;
    }
}

Related

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