String Token Replacer in C#
By FoxLearn 1/10/2025 2:42:07 AM 59
To create a string token replacer in C#, you can implement a simple method that searches for specific tokens (placeholders) in a string and replaces them with corresponding values from a dictionary.
Imagine you have a template text where you need to replace placeholders with actual values:
Hello {{name}}! Your order number {{orderNumber}} is ready for shipment.
In this example, the tokens inside double curly braces ({{token}}) are placeholders that will be replaced with real values.
Here’s how you can create a simple token replacer in C#.
using System; using System.Collections.Generic; namespace TokenReplacerExample { public static class TokenReplacer { // Method to replace tokens in a string using a dictionary of tokens public static string ReplaceTokens(this string template, Dictionary<string, string> tokenValues) { if (tokenValues == null) return template; // Loop through each token in the dictionary and replace the placeholder in the string foreach (var token in tokenValues) { // Replace the token placeholder with the actual value template = template.Replace("{{" + token.Key + "}}", token.Value); } return template; } } class Program { static void Main(string[] args) { // Dictionary of tokens and their corresponding replacement values var tokenValues = new Dictionary<string, string> { { "name", "Alice" }, { "orderNumber", "987654" }, { "shippingDate", "January 15, 2025" } }; // Template string with tokens to be replaced string template = "Hello {{name}}, your order {{orderNumber}} will be shipped on {{shippingDate}}."; // Replace tokens using the ReplaceTokens method string result = template.ReplaceTokens(tokenValues); // Output the result Console.WriteLine(result); } } }
In this example:
ReplaceTokens
method: This extension method for strings takes a dictionary where keys are the token names (e.g.,name
,orderNumber
), and the values are the replacement strings. It loops through the dictionary, replacing each{{token}}
in the string with the corresponding value.- Template String: The string you want to modify has placeholders like
{{name}}
,{{orderNumber}}
, etc., which are replaced by the actual values in the dictionary.
Given the following dictionary:
var tokenValues = new Dictionary<string, string> { { "name", "Alice" }, { "orderNumber", "987654" }, { "shippingDate", "January 15, 2025" } };
And the template string:
string template = "Hello {{name}}, your order {{orderNumber}} will be shipped on {{shippingDate}}.";
The output would be:
Hello Alice, your order 987654 will be shipped on January 15, 2025.
This method is very flexible and allows you to easily replace tokens in any string with the corresponding values from a dictionary.
- How to fix 'Failure sending mail' in C#
- How to Parse a Comma-Separated String from App.config in C#
- How to convert a dictionary to a list in C#
- How to retrieve the Executable Path in C#
- How to validate an IP address in C#
- How to retrieve the Downloads Directory Path in C#
- C# Tutorial
- Dictionary with multiple values per key in C#
Categories
Popular Posts
AdminKit Bootstrap 5 HTML5 UI Kits Template
11/17/2024
Spica Admin Dashboard Template
11/18/2024