温馨提示×

c# writeline支持自定义格式吗

c#
小樊
87
2024-11-25 12:09:43
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

是的,C# 的 WriteLine 方法支持自定义格式。你可以使用占位符 {0}{1} 等来表示参数,然后在调用 WriteLine 方法时传入相应的参数。例如:

int age = 25;
string name = "John";
Console.WriteLine("My name is {0} and I am {1} years old.", name, age);

这将输出:

My name is John and I am 25 years old.

你还可以使用 string.Format 方法来实现类似的功能:

int age = 25;
string name = "John";
Console.WriteLine(string.Format("My name is {0} and I am {1} years old.", name, age));

这将产生相同的输出。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:c# scanf能自定义格式吗

0