Java String Substritute substituteEnvironmentVariables(String str, String prefix, String suffix)

Here you can find the source of substituteEnvironmentVariables(String str, String prefix, String suffix)

Description

substitute Environment Variables

License

Open Source License

Declaration

public static String substituteEnvironmentVariables(String str, String prefix, String suffix) 

Method Source Code

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

public class Main {
    public static String substituteEnvironmentVariables(String str, String prefix, String suffix) {
        StringBuilder buffer = new StringBuilder(str);
        int prefixLength = prefix.length();
        int index = 0;
        while (index < buffer.length()) {
            index = buffer.indexOf(prefix, index);
            if (index < 0)
                break;
            index += prefixLength;//w  ww .  j  a v a2  s .c o m
            int startIndex = index;
            index = buffer.indexOf(suffix, index);
            if (index < 0)
                index = startIndex;
            else {
                String value = null;
                if (index > startIndex) {
                    try {
                        value = System.getenv(buffer.substring(startIndex, index));
                    } catch (SecurityException e) {
                        // ignore
                    }
                }
                startIndex -= prefixLength;
                if (value == null)
                    value = "";
                buffer.replace(startIndex, index + suffix.length(), value);
                index = startIndex + value.length();
            }
        }
        return buffer.toString();
    }
}

Related

  1. substitute(String template, String[] values)
  2. substitute(String txt, String pattern, String sub)
  3. substituteAll(String regexp, String subst, String target)
  4. substituteBlanks(String s, String subst)
  5. substituteCommandLine(String[] parsedCommandLine, String file1Name, String file2Name, String display1, String display2)
  6. substituteHex(String aString)
  7. substituteInvalidChars(String str)
  8. substituteJobID(String path)
  9. substituteMarkers(String sql, String marker, Object substitution)