/// <summary> /// Replaces tokens in the string with the corresponding values from a dictionary object. /// Use the options argument to indicate the prefix and/or suffix used to denote tokens. /// </summary> public static string ReplaceTokens(this string source, IDictionary <string, object> tokens, InfTokenOptions options) { if (options == null) { options = new InfTokenOptions(); } var replaced = source; foreach (var token in tokens) { var tokenName = token.Key; if (!string.IsNullOrWhiteSpace(options.TokenPrefix) && !tokenName.StartsWith(options.TokenPrefix, StringComparison.OrdinalIgnoreCase)) { tokenName = options.TokenPrefix + tokenName; } if (!string.IsNullOrWhiteSpace(options.TokenSuffix) && !tokenName.EndsWith(options.TokenSuffix, StringComparison.OrdinalIgnoreCase)) { tokenName = tokenName + options.TokenSuffix; } var tokenValue = string.Format(CultureInfo.InvariantCulture, "{0}", token.Value); replaced = replaced.Replace(tokenName, tokenValue, false); } return(replaced); }
/// <summary> /// Replaces tokens in the string with the corresponding values from a data row object. /// Use the options argument to indicate the prefix and/or suffix used to denote tokens. /// </summary> public static string ReplaceTokens(this string source, DataRow tokens, InfTokenOptions options) { return(source.ReplaceTokens(tokens.ToDictionary(), options)); }