Format()
方法根据传递的参数返回一个格式化的字符串。
示例
using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {
string name = "Programiz";
// format string
string strFormat = String.Format("Hello {0}", name);
Console.WriteLine(strFormat);
}
}
}
// Output: Hello Programiz
Format() 语法
字符串 Format()
方法的语法是
String.Format(String format, Object...args);
在这里,Format()
是一个静态方法。因此,我们使用类名 String
来调用它。
Format() 参数
String.Format()
方法接受两个参数
- format - 一个格式字符串
- args - 要格式化的对象
Format() 返回值
Format()
方法返回一个格式化的 string
。
示例 1:C# String Format()
// C# Program to insert the value of a single variable in a string
using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {
int number = 2;
// format string
string strFormat = String.Format("There are {0} apples.", number);
Console.WriteLine(strFormat);
}
}
}
输出
There are 2 apples.
在上面的代码中,请注意这一行
string strFormat = String.Format("There are {0} apples.", number);
这里,
"There are {0} apples."
是一个格式字符串{0}
是格式项- 变量 number 被插入到
{0}
的位置
示例 2:C# Format() 具有多个格式项
using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {
string name = "Ed Sheeran";
string food = "apple";
// format string
string strFormat = String.Format("{0} eats {1}", name, food);
Console.WriteLine(strFormat);
Console.ReadLine();
}
}
}
输出
Ed Sheeran eats apple
在上面的示例中,请注意这一行:
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) {
// format string
string strFormat = String.Format("{0, 20}", "Programiz");
Console.WriteLine(strFormat);
Console.ReadLine();
}
}
}
输出
Programiz
在上面的示例中,请注意这一行。
string strFormat = String.Format("{0, 20}", "Programiz");
这里,
0
代表方法中的第一个对象20
指定了字符串的宽度- 由于
20
是正数,"Programiz"
会右对齐
示例 4:C# Format() - 控制间距和左对齐
using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {
// format string
string strFormat = String.Format("{0, -20} {1, -14}", "Programiz", "C# Programming");
Console.WriteLine(strFormat);
Console.ReadLine();
}
}
}
输出
Programiz C# Programming
在上面的示例中,我们使用了格式字符串 "{0, -20} {1, -14}"
。这里,
- 0 和 1 分别代表方法中的第一个和第二个对象(
"Programiz"
和"C# Programming"
) - -20 和 -14 分别代表
"Programiz"
和"C# Programming"
的宽度。
由于 -20 和 -14 是负数,"Programiz"
和 "C# Programming"
会左对齐。
常见问题
如何使用 String.Format() 格式化日期?
我们可以通过以下方式使用 String.Format()
格式化日期
using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {
DateTime date = new DateTime(2015, 12, 31);
// format string
string strDate = String.Format("{0:D}", date);
Console.WriteLine(strDate);
Console.ReadLine();
}
}
}
输出
Thursday, December 31, 2015
在上面的示例中,请注意这一行:
string strDate = String.Format("{0:D}", date);
{0:D}
格式项指定了方法中传递的第一个对象将以长日期格式进行格式化。
String.Format() 中常用的日期格式说明符有哪些?
一些常用的日期格式说明符如下
格式说明符 | 描述 | 示例 |
---|---|---|
d |
短日期 | 9/17/2021 |
D |
长日期 | Friday, September 17, 2021 |
t |
短时间 | 4:11 PM |
T |
长时间 | 4:11:34 PM |
M |
月份 | September 17 |
Y |
年份 | September 2021 |
如何使用 String.Format() 格式化数字?
我们可以通过以下方式使用 String.Format()
格式化数字
using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {
// format string
string strDecimal = String.Format("Decimal: {0:D}", 200);
string strHexaDecimal = String.Format("Hexadecimal: {0:X}", 200);
Console.WriteLine(strDecimal);
Console.WriteLine(strHexaDecimal);
Console.ReadLine();
}
}
}
输出
Decimal: 200 Hexadecimal: C8
在上面的示例中,请注意这一行:
string strDecimal = String.Format("Decimal: {0:D}", 200);
string strHexaDecimal = String.Format("Hexadecimal: {0:X}", 200);
这里,
{0:D}
- 指定方法中传递的第一个对象将以十进制格式化{0:X}
- 指定方法中传递的第一个对象将以十六进制格式化
String.Format() 中常用的数字格式说明符有哪些?
一些常用的数字格式说明符如下
格式说明符 | 描述 | 示例 |
---|---|---|
N |
数字 | 200.00 |
E |
科学计数法 | 2.000000E+002 |
C |
货币 | $200.00 |
P |
百分比 | 20,000.00% |