在C#中,字符串格式化的方法主要有两种:使用字符串插值和使用String.Format()方法。
string name = "Alice";
int age = 30;
string message = $"My name is {name} and I am {age} years old.";
Console.WriteLine(message);
string name = "Bob";
int age = 25;
string message = String.Format("My name is {0} and I am {1} years old.", name, age);
Console.WriteLine(message);
这两种方法都可以用来格式化字符串,选择使用哪种方法取决于个人偏好和代码风格。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>