9

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.

2
  • 1
    can you give an example of a desired input/output? Commented Jun 10, 2013 at 17:07
  • can you clearify more about the case that you want to handle ? Commented Jun 10, 2013 at 17:08

8 Answers 8

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" }); 
Sign up to request clarification or add additional context in comments.

5 Comments

May I know what framework to allow use String.Inject property? I'm use version 3.5 where the String.Inject property is not avaible
@Chuki2: You need to include the class listed on that page in your project, then add it as a using statement.
@BradChristie, sorry, I did not realize about that. Let me try
@BradChristie, Simple and awesome.. Thanks for help!
@AzriZakaria this method is a custom class and it is not part of C# String class methods.
8
var newString = messageSms.Replace("{Company}", CompanyName) .Replace("{CustomerName}", CustomerName) // ...etc 

Should do it.

Comments

6

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}."; 

Comments

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. "; 

Comments

2

Try String.Format Method, for example:

string messageSms = String.Format("From {0}. Hi ..{1}, Your..{2} with..{3}. We..", CompanyName, CustomerName, BillNumber, Payment); 

2 Comments

This doesn't match the OPs' "named parameter" requirement.
Yes that true, as programmer easy to understand it but not for user. That's why i need use named parameter. By the way thanks for help.
1

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); 

1 Comment

For user,with this way is not friendly and can make confuse. Thanks for help
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(); 

Comments

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(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.