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

标签 c# string

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

我创建了这个变量

  1. 公司名称
  2. 客户姓名
  3. 账单号码
  4. 付款

示例:

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.

我当前的代码适用于静态消息,

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但帮助不大。

最佳答案

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

应该这样做。

关于c# - C#中动态字符串中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17029205/

相关文章:

c# - 内部抽象类 : how to hide usage outside assembly?

c - 如何在 C 中创建字符串数组?

python - 迭代文本文件中的行,返回行号和出现次数?

java - Unicode 到 Java 中的字符串转换

c# - 使用模型值包括报价 ASP MVC4

c# - 使用MVVM Light和PCL创建UWP应用程序

c# - 如何对测试( View 模型)类中的方法调用进行单元测试?

c# - Dapper.Contrib 和 MiniProfiler(适用于 MySql)集成问题

string - 如何在 Flutter 中的 Text Widget 中将 double 转换为字符串

c++ - 将 int 转换为 base 2 cstring/string