在C#中,string.Format 方法用于创建一个格式化的字符串,并替换字符串中的占位符(格式化项)为指定的参数值。其基本语法如下:
string formattedString = string.Format(format, arg0, arg1, ...);
其中:
format 是包含格式化项的字符串,格式化项以花括号 {} 包围,如 {0}, {1}, {2}, 等等。arg0, arg1, … 是要替换的参数值,根据对应的格式化项顺序传入。例如:
string name = "Alice";
int age = 30;
string message = string.Format("My name is {0} and I am {1} years old.", name, age);
在这个例子中,{0} 会被 name 的值替换,{1} 会被 age 的值替换,最终得到的 message 字符串为:“My name is Alice and I am 30 years old.”