c#

c#中string.format的用法是什么

小亿
83
2024-03-16 13:50:42
栏目: 编程语言

在C#中,string.Format 方法用于创建一个格式化的字符串,并替换字符串中的占位符(格式化项)为指定的参数值。其基本语法如下:

string formattedString = string.Format(format, 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.”

0
看了该问题的人还看了