为什么要使用String.Format?[重复]

字符串输出:C#中的格式还是concat? (31个答案)

7年前关闭。

为什么有人会用String.FormatC#和VB .NET而不是串联运算符(&在VB和+C#中)?

主要区别是什么?为什么每个人都对使用如此感兴趣String.Format?我很好奇。

回答:

我可以看到许多原因:

string s = string.Format("Hey, {0} it is the {1}st day of {2}.  I feel {3}!", _name, _day, _month, _feeling);

vs:

string s = "Hey," + _name + " it is the " + _day + "st day of " + _month + ".  I feel " + feeling + "!";

(这包括您可以编写自定义格式符的事实)

string s = string.Format("Invoice number: {0:0000}", _invoiceNum);

vs:

string s = "Invoice Number = " + ("0000" + _invoiceNum).Substr(..... /*can't even be bothered to type it*/)

如果我要将字符串模板存储在数据库中怎么办?使用字符串格式:

_id         _translation

1 Welcome {0} to {1}. Today is {2}.

2 You have {0} products in your basket.

3 Thank-you for your order. Your {0} will arrive in {1} working days.

vs:

_id         _translation

1 Welcome

2 to

3 . Today is

4 .

5 You have

6 products in your basket.

7 Someone

8 just shoot

9 the developer.

以上是 为什么要使用String.Format?[重复] 的全部内容, 来源链接: utcz.com/qa/409058.html

回到顶部