8

I have a module to send message with the SMS. I can put the variable in the string if the message is a static, but the user request the message can be changed whatever their want.

I created this variable

  1. CompanyName
  2. CustomerName
  3. BillNumber
  4. Payment

Example :

From {Company}. Hi Mr/Mrs {CustomerName}, your bill number is {BillNumber} with a total payment of {Payment}. We want to inform you the items has been completed and ready for collection.

My current code is work for static message,

string messageSms = "From " +Company+ ". Hi Mr/Mrs "+{CustomerName}+", your bill number is "+{BillNumber}+" with a total payment of "+{Payment}+". We want to inform you the items has been completed and ready for collection.";

But how can be done with dynamic message? How can I detect the variable in the string and set the data on the variable?

I also following with this article but not help so much.

Community
  • 1
  • 1
Azri Zakaria
  • 1,324
  • 5
  • 23
  • 52

8 Answers8

8
var newString = messageSms.Replace("{Company}", CompanyName)
                          .Replace("{CustomerName}", CustomerName) // ...etc

Should do it.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
8

Assuming I'm understanding, i think the String.Inject class could be helpful. Picture a named String.Format:

"Hello, {company}!".Inject(new { company = "StackOverflow" });
// "Hello, StackOverflow!"

The other benefit is you can have a hard-coded model and reference direct properties of it. e.g.

class Contact
{
    string FirstName;
    string LastName;
}

String greeting = "Mr. {FirstName} {LastName}, Welcome to the site ...";
String result = greeting.Inject(new Contact
{
    FirstName = "Brad",
    LastName = "Christie"
});
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
5

You could also use interpolated strings using C# 6.0

var messageSms = $"From {companyName}. Hi {customerName}, your bill number is {billNumber} with a total payment of {payment}.";
katronai
  • 540
  • 1
  • 8
  • 25
4

I would approach with the following :

string Company;
string CustomerName;
string BillNumber;
string Payment;

string messageSms = $@"
From {Company}. Hi Mr/Mrs {CustomerName}, your bill number is {BillNumber} 
with a total payment of {Payment}. We want to inform you the items has been 
completed and ready for collection.
";
radiopad
  • 41
  • 2
2

Try String.Format Method, for example:

string messageSms = String.Format("From {0}. Hi ..{1}, Your..{2} with..{3}. We..", 
                                  CompanyName, CustomerName, BillNumber, Payment);
Kaf
  • 33,101
  • 7
  • 58
  • 78
0

I assume that the easiest way to achieve this (you did not clarify your question) is to use string.Format(). Just use it like this:

string company = ...;
string name= ...;
string billNr= ...;
string payment= ...;
string output = string.Format("From {0}. Hi Mr/Mrs {1}, your bill number is {2} with a total payment of {3}. We want to inform you the items has been completed and ready for collection.", company, name, billNr, payment);
wojtuch
  • 188
  • 2
  • 11
0

Why not use a StringBuilder instead?

StringBuilder stringBuilder = new StringBuilder("From {Company}.Hi Mr/Mrs {CustomerName}, your bill number is {BillNumber} with a total payment of {Payment}. We want to inform you the items has been completed and ready for collection.");
stringBuilder.Replace("{Company}",CompanyName);
stringBuilder.Replace("{CustomerName}",CustomerName);
stringBuilder.Replace("{BillNumber}",BillNumber);
stringBuilder.Replace("{Payment}",Payment);
string messageSms = stringBuilder.ToString();
Bhushan Firake
  • 9,338
  • 5
  • 44
  • 79
0

To make a reusable solution you could start by declaring an object that contains the replacement values as properties. In this case I simply declare an anonymous object but a normal class would work just as well:

var data = new {
  Company = "Stack Overflow",
  CustomerName = "Martin Liversage",
  BillNumber = 123456,
  Payment = 1234.567M.ToString("N2")
};

Notic how I "cheat" and assign a string to Payment. Number and date/time formatting is always a complex issue and I have decided to do the formatting up-front when I declare the data object. You could instead build some more or less elaborate formatting rules into the formatting engine.

Having a data object with properties I can build a dictionary of name/value pairs:

var dictionary = data
  .GetType()
  .GetProperties()
  .ToDictionary(
    propertyInfo => propertyInfo.Name,
    propertyInfo => propertyInfo.GetValue(data, null)
  );

Assuming that format contains the formatting template it is simply a matter of looping over the elements in the dictionary to create the replacement string:

var buffer = new StringBuilder(format);
foreach (var name in dictionary.Keys) {
  var value = dictionary[name].ToString();
  buffer.Replace("{" + name + "}", value);
}
var message = buffer.ToString();
Martin Liversage
  • 104,481
  • 22
  • 209
  • 256