我有一个用 SMS 发送消息的模块。如果消息是静态的,我可以将变量放在字符串中,但用户请求可以根据需要更改消息。
我创建了这个变量
- 公司名称
- 客户姓名
- 账单号码
- 付款
示例:
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/