Linux 拨号vps windows公众号手机端

c#中string.format方法怎么使用

lewis 8年前 (2017-04-09) 阅读数 7 #程序编程
文章标签 c#

C#中的string.Format方法用于将指定的格式应用于字符串中的参数。以下是使用string.Format方法的基本语法:

string result = string.Format(format, arg0, arg1, ...);
  • format是一个字符串,用于指定输出的格式。可以使用占位符 {0}, {1}, 等将参数插入到字符串中。
  • arg0, arg1, … 是要插入到字符串中的参数。

以下是一个示例,演示如何使用string.Format方法:

string name = "John";
int age = 25;
string message = string.Format("My name is {0} and I am {1} years old.", name, age);
Console.WriteLine(message);

输出结果为:

My name is John and I am 25 years old.

在上面的示例中,我们使用{0}{1}作为占位符,并将nameage参数作为string.Format的参数传递。

版权声明

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

发表评论:

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

热门