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
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.
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));
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.
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.
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.
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"
});
var newString = messageSms.Replace("{Company}", CompanyName)
.Replace("{CustomerName}", CustomerName) // ...etc
Should do it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With