Here you can find the source of replace(String string, Properties table)
replace(string, table, false)
.
Parameter | Description |
---|---|
string | the String to be processed. |
table | the replacement strings |
public static String replace(String string, Properties table)
//package com.java2s; /*--------------------------------------------------------------- * Copyright 2005 by the Radiological Society of North America * * This source software is released under the terms of the * RSNA Public License (http://mirc.rsna.org/rsnapubliclicense) *----------------------------------------------------------------*/ import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { /**//from ww w . j av a2 s . com * Equivalent to <code>replace(string, null, true)</code>. * @param string the String to be processed. * @return a new string with the identifiers replace with values * from the system environment. */ public static String replace(String string) { return replace(string, null, true); } /** * Equivalent to <code>replace(string, table, false)</code>. * @param string the String to be processed. * @param table the replacement strings * @return a new string with the identifiers replaced with values * from the table. */ public static String replace(String string, Properties table) { return replace(string, table, false); } /** * Replace coded identifiers with values from a table. * Identifiers are coded as ${name}. The identifier is replaced * by the string value in the table, using the name as the key. * Identifiers which are not present in the table are left * unmodified. * @param string the String to be processed * @param table the replacement strings (this argument can be null) * @param includeEnvironmentVariables true to include the System * environment variables in the replacement table * @return a new string with the identifiers replaced with values * from the table. */ public static String replace(String string, Properties table, boolean includeEnvironmentVariables) { try { Pattern pattern = Pattern.compile("\\$\\{\\w+\\}"); Matcher matcher = pattern.matcher(string); StringBuffer sb = new StringBuffer(); while (matcher.find()) { String group = matcher.group(); String key = group.substring(2, group.length() - 1).trim(); String repl = null; if (table != null) repl = table.getProperty(key); if ((repl == null) && includeEnvironmentVariables) repl = System.getenv(key); if (repl == null) repl = Matcher.quoteReplacement(group); matcher.appendReplacement(sb, repl); } matcher.appendTail(sb); return sb.toString(); } catch (Exception ex) { return string; } } /** * Trim a string, returning the empty string if the supplied string is null. * @param text the text to trim. * @return the trimmed string. */ public static String trim(String text) { if (text == null) return ""; return text.trim(); } }