C#中动态字符串中的变量

时间:2013-06-10 17:06:02

标签: c# string

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

我创建了这个变量

  1. CompanyName
  2. 客户名称
  3. BillNumber
  4. 付款
  5. 示例:

      

    来自 {Company} 。先生/女士 {客户名称} ,您的账单号是    {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.";
    

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

    我也关注这个article,但没有那么多帮助。

8 个答案:

答案 0 :(得分:6)

假设我理解,我认为String.Inject课程可能会有所帮助。想象出一个名为String.Format

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

另一个好处是你可以拥有一个硬编码模型并引用它的直接属性。 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"
});

答案 1 :(得分:4)

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

应该这样做。

答案 2 :(得分:2)

您也可以使用C#6.0

使用interpolated strings
var messageSms = $"From {companyName}. Hi {customerName}, your bill number is {billNumber} with a total payment of {payment}.";

答案 3 :(得分:1)

我会接近以下内容:

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

答案 4 :(得分:0)

尝试String.Format方法,例如:

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

答案 5 :(得分: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);

答案 6 :(得分:0)

为什么不改用StringBuilder

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

答案 7 :(得分:0)

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

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

Notic我如何“作弊”并将字符串分配给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();