6

我有一个用 SMS 发送消息的模块。如果消息是静态的,我可以将变量放在字符串中,但是用户请求可以随意更改消息。

我创建了这个变量

  1. 公司名称
  2. 顾客姓名
  3. 帐单号码
  4. 支付

例子 :

来自{公司}。您好,{CustomerName}先生/夫人,您的帐单号码是 {BillNumber},总付款为{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.";

但是如何处理动态消息呢?如何检测字符串中的变量并在变量上设置数据?

我也关注这篇文章,但没有太大帮助。

4

8 回答 8

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

Should do it.

于 2013-06-10T17:08:14.987 回答
6

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"
});
于 2013-06-10T17:08:56.707 回答
3

我会采用以下方法:

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.
";
于 2017-09-26T13:09:28.707 回答
3

您还可以使用 C# 6.0使用内插字符串

var messageSms = $"From {companyName}. Hi {customerName}, your bill number is {billNumber} with a total payment of {payment}.";
于 2017-03-22T12:06:18.790 回答
1

尝试String.Format方法,例如:

string messageSms = String.Format("From {0}. Hi ..{1}, Your..{2} with..{3}. We..", 
                                  CompanyName, CustomerName, BillNumber, Payment);
于 2013-06-10T17:10:33.517 回答
0

我认为实现这一目标的最简单方法(您没有澄清您的问题)是使用string.Format(). 像这样使用它:

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);
于 2013-06-10T17:13:34.170 回答
0

要制作可重用的解决方案,您可以首先声明一个包含替换值作为属性的对象。在这种情况下,我只是声明了一个匿名对象,但普通类也可以工作:

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

请注意我是如何“作弊”并将字符串分配给Payment. 数字和日期/时间格式始终是一个复杂的问题,我决定在声明数据对象时预先进行格式设置。相反,您可以在格式化引擎中构建一些或多或少复杂的格式化规则。

拥有一个具有属性的数据对象,我可以构建一个名称/值对的字典:

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

假设它format包含格式化模板,只需遍历字典中的元素以创建替换字符串:

var buffer = new StringBuilder(format);
foreach (var name in dictionary.Keys) {
  var value = dictionary[name].ToString();
  buffer.Replace("{" + name + "}", value);
}
var message = buffer.ToString();
于 2013-06-10T17:33:07.030 回答
0

为什么不使用 aStringBuilder代替?

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();
于 2013-06-10T17:18:52.847 回答