Linux 拨号vps windows公众号手机端

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

lewis 9年前 (2016-10-17) 阅读数 8 #程序编程
文章标签 c#

在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.”

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门