跳到主要内容

C# String Format() 字符串格式化方法

Format() 方法基于传入的参数返回格式化后的字符串。

示例

using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {

string name = "mashangxue123";
// 格式化字符串
string strFormat = String.Format("你好 {0}", name);
Console.WriteLine(strFormat);
}
}
}
// 输出: 你好 mashangxue123

Format() 语法

字符串 Format() 方法的语法如下:

String.Format(String format, Object...args);

这里,Format() 是一个静态方法。因此,我们使用类名 String 来调用它。

Format() 参数

String.Format() 方法接受两个参数:

  • format - 格式化字符串
  • args - 要格式化的对象

Format() 返回值

Format() 方法返回一个格式化后的 string

示例1:C# String Format()

// C#程序将单个变量的值插入字符串中

using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {

int number = 2;
// 格式化字符串
string strFormat = String.Format("有 {0} 个苹果。", number);
Console.WriteLine(strFormat);
}
}
}

输出

2 个苹果。

在上面的代码中,注意到这一行:

string strFormat = String.Format("有 {0} 个苹果。", number);

这里,

  • "有 {0} 个苹果。" 是格式化字符串
  • {0} 是格式项
  • 变量 number 被插入到 {0} 的位置

示例2:C# Format() 带有多个格式项

using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {

string name = "Ed Sheeran";
string food = "苹果";

// 格式化字符串
string strFormat = String.Format("{0} 喜欢吃 {1}", name, food);
Console.WriteLine(strFormat);

Console.ReadLine();
}
}
}

输出

Ed Sheeran 喜欢吃 苹果

在上述示例中,请注意这一行,

string strFormat = String.Format("{0} eats {1}", name, food);

这里,

  • 我们有两个格式项 {0}{1}
  • {0} 被方法中传入的第一个对象即 name 替换
  • {1} 被方法中传入的第二个对象即 food 替换

示例 3:C# Format() - 控制间距和右对齐

using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {

// 格式化字符串
string strFormat = String.Format("{0, 20}", "mashangxue123");
Console.WriteLine(strFormat);

Console.ReadLine();
}
}
}

输出

       	mashangxue123

在以下示例中,请注意这一行。

string strFormat = String.Format("{0, 20}", "mashangxue123");

这里,

  • 0 代表方法中的第一个对象
  • 20 指定字符串的宽度
  • 由于 20 是一个正数,"mashangxue123" 是右对齐的

示例 4:C# Format() - 控制间距和左对齐

using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {

// 格式化字符串
string strFormat = String.Format("{0, -20} {1, -14}", "mashangxue123", "C# 编程");
Console.WriteLine(strFormat);

Console.ReadLine();
}
}
}

输出

mashangxue123        	C# 编程

在上面的示例中,我们使用了格式字符串 "{0, -20} {1, -14}"。这里,

  • 01 代表方法中的第一个和第二个对象("mashangxue123""C# 编程"
  • -20-14 分别代表 "mashangxue123""C# 编程" 的宽度。

由于 -20-14 是负数,"mashangxue123""C# 编程" 是左对齐的。

常见问题解答

如何使用 String.Format() 格式化日期?

我们可以使用 String.Format() 以以下方式格式化日期:

using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {

DateTime date = new DateTime(2015, 12, 31);

// 格式化字符串
string strDate = String.Format("{0:D}", date);
Console.WriteLine(strDate);

Console.ReadLine();
}
}
}

输出

20151231日星期四

在上面的示例中,请注意以下行:

string strDate = String.Format("{0:D}", date);

{0:D} 格式项指定将方法中传递的第一个对象格式化为长日期格式。

String.Format() 中常见的日期格式说明符有哪些?

一些常见的日期格式说明符如下:

格式说明符描述示例
d短日期格式9/17/2021
D长日期格式2021年9月17日 星期五
t短时间格式下午4:11
T长时间格式下午4:11:34
M月份格式9月17日
Y年份格式2021年9月

如何使用 String.Format() 格式化数字?

我们可以使用 String.Format() 如下所示格式化数字:

using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {

// 格式化字符串
string strDecimal = String.Format("十进制: {0:D}", 200);
string strHexaDecimal = String.Format("十六进制: {0:X}", 200);

Console.WriteLine(strDecimal);
Console.WriteLine(strHexaDecimal);

Console.ReadLine();
}
}
}

输出

十进制: 200
十六进制: C8

在上面的示例中,请注意以下行:

string strDecimal = String.Format("十进制: {0:D}", 200);

string strHexaDecimal = String.Format("十六进制: {0:X}", 200);

这里,

  • {0:D} - 指定方法中传递的第一个对象将以十进制格式进行格式化
  • {0:X} - 指定方法中传递的第一个对象将以十六进制格式进行格式化

String.Format() 中常见的数字格式说明符有哪些?

一些常见的数字格式说明符如下:

格式说明符描述示例
N数字格式200.00
E科学计数法2.000000E+002
C货币格式$200.00
P百分比格式20,000.00%