Java String Substritute substitute(String str, CharSequence... substitutionSeqs)

Here you can find the source of substitute(String str, CharSequence... substitutionSeqs)

Description

substitute

License

Open Source License

Declaration

public static String substitute(String str, CharSequence... substitutionSeqs) 

Method Source Code

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

public class Main {
    public static final char SUBSTITUTION_PLACEHOLDER_PREFIX_CHAR = '%';
    private static final int MIN_SUBSTITUTION_INDEX = 1;

    public static String substitute(String str, CharSequence... substitutionSeqs) {
        StringBuilder buffer = new StringBuilder(str.length() + 32);
        int index = 0;
        while (index < str.length()) {
            int startIndex = index;
            index = str.indexOf(SUBSTITUTION_PLACEHOLDER_PREFIX_CHAR, index);
            if (index < 0)
                index = str.length();//  ww w. j  av  a 2  s.c o  m
            if (index > startIndex)
                buffer.append(str.substring(startIndex, index));
            ++index;

            if ((index < str.length()) && (str.charAt(index) == SUBSTITUTION_PLACEHOLDER_PREFIX_CHAR)) {
                buffer.append(SUBSTITUTION_PLACEHOLDER_PREFIX_CHAR);
                ++index;
            } else {
                startIndex = index;
                while (index < str.length()) {
                    char ch = str.charAt(index);
                    if ((ch < '0') || (ch > '9'))
                        break;
                    ++index;
                }
                if (index > startIndex) {
                    int substIndex = Integer.parseInt(str.substring(startIndex, index)) - MIN_SUBSTITUTION_INDEX;
                    if ((substIndex >= 0) && (substIndex < substitutionSeqs.length)) {
                        CharSequence substSeq = substitutionSeqs[substIndex];
                        if (substSeq != null)
                            buffer.append(substSeq);
                    }
                }
            }
        }
        return buffer.toString();
    }
}

Related

  1. substitute(final String input, final String pattern, final String sub)
  2. substitute(String in, String find, String newString)
  3. substitute(String original, String match, String subst)
  4. substitute(String s, String from, String to)
  5. substitute(String str, String from, String to)
  6. substitute(String str, String source, String target)
  7. substitute(String str, String source, String target)
  8. substitute(String str, String variable, String value, int num)