我很好奇为什么我会使用字符串格式,而我可以使用连接,例如
I m curious why would i use string formatting while i can use concatenation such as
Console.WriteLine("Hello {0} !", name);
Console.WriteLine("Hello "+ name + " !");
为什么更喜欢第一个而不是第二个?
Why to prefer the first one over second?
你选的例子太简单了.
字符串格式:
("{0} + {0} = {1}", x, 2*x)
ToString
:("{0}: {1}", someKeyObj, someValueObj)
("The value will be {0:3N} (or {1:P}) on {2:MMMM yyyy gg}", x, y, theDate)
(">{0,3}<", "hi");//">hi <"
这篇关于c# 字符串格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!