Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable in the dynamic string in C#

Tags:

string

c#

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.

like image 394
Azri Zakaria Avatar asked Jun 10 '13 17:06

Azri Zakaria


People also ask

How do you declare a string dynamically?

Allocating Strings DynamicallyEdit In duplicating a string, s, for example we would need to find the length of that string: int len = strlen(s); And then allocate the same amount of space plus one for the terminator and create a variable that points to that area in memory: char *s2 = malloc((len + 1) * sizeof(char));

What is a dynamic string?

Strings in C are defined as a stream of contiguous bytes, terminated by a byte with the value zero. The C standard library has many functions that deal with this type of string, but they suffer from one major problem.

What is a dynamic array in C?

Dynamic arrays are resizable and provide random access for their elements. They can be initialized with variable size, and their size can be modified later in the program. Dynamic arrays are allocated on the heap whereas VLAs are allocated on the stack.

How do you allocate space in a string?

Space is allocated by calling malloc with the number of bytes needed (for strings this is always one more than the maximum length of the string to be stored): char *pc = malloc(MAXSTR + 1) ; // can hold a string of up to MAXSTR characters.


2 Answers

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"
});
like image 187
Brad Christie Avatar answered Oct 08 '22 16:10

Brad Christie


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

Should do it.

like image 32
Robert Harvey Avatar answered Oct 08 '22 16:10

Robert Harvey