The TokenUtility
class, implemented within the TokenHandler.Utilities
namespace, provides utility methods for working with tokens embedded in strings. It supports extracting tokens in the format {{TokenName}}
and replacing them with values provided in a dictionary.
- Extract Tokens: Identify and retrieve all tokens formatted as
{{TokenName}}
from a given string. - Replace Tokens: Dynamically replace tokens in a string with single or multiple values, with optional support for looping through templates using
loopSource
.
namespace TokenHandler.Utilities;
ITokenHandler
The class adheres to the ITokenHandler
interface, ensuring flexibility and easy integration.
Extracts all tokens from a given string.
public List<string> ExtractToken(string body)
body
: The input string containing tokens to extract.
- Uses a regex pattern
@"\{\{([^\}]+)\}\}"
to identify tokens enclosed in{{ }}
. - Returns a list of tokens, including the enclosing braces.
string body = "Hello {{FirstName}}, your balance is {{Balance}}.";
TokenUtility utility = new TokenUtility();
List<string> tokens = utility.ExtractToken(body);
// tokens = ["{{FirstName}}", "{{Balance}}"]
Replaces tokens in a string with their corresponding values from a dictionary.
public string ReplaceTokens(string body, Dictionary<string, string> tokenValues, string? loopSource = null)
body
: The string containing tokens to replace.tokenValues
: A dictionary where:- Keys are tokens (e.g.,
{{TokenName}}
). - Values are replacements (e.g.,
"John"
for{{FirstName}}
).
- Keys are tokens (e.g.,
loopSource
(optional): A JSON string representing a list of templates for handling tokens with multiple values.
-
Single Value Replacement:
- Replaces tokens with their respective single value from
tokenValues
.
- Replaces tokens with their respective single value from
-
Multiple Value Replacement:
- Splits comma-separated values for tokens.
- Uses the
loopSource
parameter to format replacements dynamically. - Appends the results for all values into a single string.
A string with all tokens replaced by their respective values.
string body = "Welcome, {{UserName}}!";
var tokenValues = new Dictionary<string, string> { { "{{UserName}}", "Alice" } };
TokenUtility utility = new TokenUtility();
string result = utility.ReplaceTokens(body, tokenValues);
// Output: "Welcome, Alice!"
string body = "<table>{{RowTemplate}}</table>";
var tokenValues = new Dictionary<string, string>
{
{ "{{RowTemplate}}", "Row1,Row2,Row3" }
};
string loopSource = "[\"<tr>{{RowTemplate}}</tr>\"]";
TokenUtility utility = new TokenUtility();
string result = utility.ReplaceTokens(body, tokenValues, loopSource);
// Output: "<table><tr>Row1</tr><tr>Row2</tr><tr>Row3</tr></table>"
System.Text.Json
:- Used for deserializing the
loopSource
JSON string.
- Used for deserializing the
System.Text.RegularExpressions
:- Used for regex matching of token patterns.
- Language: C#
- Framework: .NET Core or later.
- Tokens must be enclosed in
{{ }}
to be recognized by theExtractToken
method. - Ensure
loopSource
is a valid JSON-encoded string representing a list of templates for multiple-value replacements. - Handles both single and multiple values seamlessly, with support for dynamic templates.
The TokenUtility
class is open for use in personal and professional projects. Contributions and enhancements are welcome.