在C#中,string.Format()方法用于格式化字符串。它接受两个参数:格式字符串和要格式化的对象(或值)。下面是使用string.Format()的一些示例:
string name = "Alice";int age = 30;string message = string.Format("My name is {0} and I am {1} years old.", name, age);Console.WriteLine(message); // 输出:"My name is Alice and I am 30 years old."格式化数字:int number = 12345;string formattedNumber = string.Format("The number is: {0:N}", number);Console.WriteLine(formattedNumber); // 输出:"The number is: 12,345.00"格式化日期:DateTime currentDate = DateTime.Now;string formattedDate = string.Format("Today's date is: {0:D}", currentDate);Console.WriteLine(formattedDate); // 输出:"Today's date is: Monday, September 20, 2021"格式化货币:decimal price = 99.99m;string formattedPrice = string.Format("The price is: {0:C}", price);Console.WriteLine(formattedPrice); // 输出:"The price is: $99.99"这些只是string.Format()方法的一些常见用法,你可以根据需要使用不同的格式字符串和参数。

