Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8817811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:56:33+00:00 2026-06-14T04:56:33+00:00

I have a string with a message containing some fields I want to swap

  • 0

I have a string with a message containing some fields I want to swap out to actual values

var message = "Hi [CustomerName]. Its [TODAY], nice to see you were born on the [DOB]!";
var mappingCodes = new List<string> {"[CUSTOMER_NAME]","[DOB]",[TODAY]};
var customEmails = new Dictionary<string, string>();
var today = DateTime.Now;
var customers = new List<Customer>()
{
    new Customer()
        {
            FirstName = "Jo",
            LastName = "Bloggs",
            Email = "jo@bloggs.com",
            DOB = "10/12/1960"
        }
};
foreach (var customer in customers)
{
    var emailMessage = "";
    customEmails.Add(customer.Email,emailMessage);
}

What I’m trying to do is loop through each of the customers and take the message replacing any of the mappingCodes with actual codes.

e.g. [Today] Would be the today and CustomerName would be Customer.FirstName + Customer.LastName

There could be 1000’s of customers so I need something robust. I’m just not sure how to first check the string contains any of the mappingCodes and then replace them with the desired values.

Any advice?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:56:34+00:00Added an answer on June 14, 2026 at 4:56 am

    You can use Regex.Replace(string, MatchEvaluator):

    var customers = new[] {
        new {
            Name = "Fred Flintstone",
            City = "Bedrock"
        },
        new {
            Name = "George Jetson",
            City = "Orbit City"
        }
    };
    
    string template = "Hello, [NAME] from [CITY]!";
    var re = new Regex(@"\[\w+\]"); // look for all "words" in square brackets
    
    foreach (var customer in customers)
    {
        Trace.WriteLine(
            re.Replace(template, m => {
                // determine the replacement string
                switch (m.Value) // m.Value is the substring that matches the RE.
                {
                    // Handle getting and formatting the properties here
                    case "[NAME]":
                        return customer.Name;
                    case "[CITY]":
                        return customer.City;
                    default:
                        // The "mapping code" is not supported, I just return the 
                        // original substring
                        return m.Value;
                }
            }));
    }
    

    Obviously the above is just the general approach, you’ll have to modify it to support your mapping codes and data structures.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object, and a string containing a message I would like to
I have a string containing field/value pairs in xml format and want to parse
I have the following code: public void post(String message) { final String mess =
I have this delegate in C#. public delegate string ACSharpDelegate(string message); How would I
Suppose I have a function like this (I use akka 2.0.2): def foo(message: String):
I have a MDB (Message driven bean) that receives messages with String which represent
Assuming the message argument is a string, I have the following snippet: users.each do
I have a jArray and a string from that jArray, message2: [{date:2012,count:1,message : message1},
Lets say I have some random .cs file containing a class with some properties
I have some nested tables in a containing column table. The bottom table just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.