C#中的string.Format方法是一個用于格式化字符串的功能強大的方法。它允許您通過將占位符替換為實際的值來創建格式化的字符串。
一、基本語法是:
string.Format(format, arg0, arg1, arg2, ...)
其中,
format
是一個字符串,其中包含要格式化的文本和占位符。占位符使用花括號?{}
?括起來,并可以包含格式說明符,用于定義值的顯示方式。arg0
,?arg1
,?arg2
, ... 是要插入到格式字符串中的參數。這些參數將按照順序替換占位符
二、基本用法:
1、 把相關參數格式化到指定位置。
string name = "小明";
int age = 25;
string message = string.Format("我是{0},今年{1}歲。", name, age);
Console.WriteLine(message);// 我是小明,今年25歲。
2、格式化數字:
int number = 12345;
string formattedNumber = string.Format("{0:N}", number);
Console.WriteLine(formattedNumber);// 12,345.00
3、使用命名參數:
string firstName = "王";
string lastName = "小小";
string fullName = string.Format("我的名字是{firstName} {lastName}。", new { firstName, lastName });
Console.WriteLine(fullName);// 我的名字是王小小。
4、格式化日期:
DateTime currentDate = DateTime.Now;
string formattedDate = string.Format("Today is {0:yyyy-MM-dd}.", currentDate);
Console.WriteLine(formattedDate);//Today is 2023-11-23.
三、常見的一些占位符的使用。
1、{0}
?- 將第一個參數的值插入到占位符中,{1},{2}...類推。
string name = "John";
string message = string.Format("Hello, {0}!", name);
Console.WriteLine(message); //Hello, John!
2、{{0:#.##}
?- 將第一個參數的值按照指定的數值格式進行格式化
double number = 123.456;
string formattedNumber = string.Format("The number is {0:0.00}.", number);
Console.WriteLine(formattedNumber); //The number is 123.46.
3、{0,-10},
{0,10}?- 將第一個參數的值插入到占位符中,并左對齊、右對齊,總寬度為10個字符
string name = "Bob";
string formattedName = string.Format("Name: {0,-10}.", name);//左對齊
Console.WriteLine(formattedName); //Name: Bob .
?4、{0:G}
?- 將第一個參數的值按照一般格式進行格式化
DateTime now = DateTime.Now;
string formattedDate = string.Format("Current date and time: {0:G}.", now);
Console.WriteLine(formattedDate); //Current date and time: 11/23/2023 9:28:46 PM.
5、{{0:C}
?- 將第一個參數的值按照貨幣格式進行格式化(輸出人民幣還是美元跟系統有關)
decimal price = 99.99m;
string formattedPrice = string.Format("Price: {0:C}.", price);
Console.WriteLine(formattedPrice); // Price: $99.99.
6、{0:E}
?- 將第一個參數的值按照科學計數法格式進行格式化
double number = 12345.6789;
string formattedNumber = string.Format("Number: {0:E}.", number);
Console.WriteLine(formattedNumber); //Number: 1.234568E+004.
7、{0:X}
?- 將第一個參數的值按照十六進制格式進行格式化
int number = 255;
string formattedNumber = string.Format("Number: {0:X}.", number);
Console.WriteLine(formattedNumber); //Number: FF.
8、{0:P}
?- 將第一個參數的值按照百分比格式進行格式化
9、{0:'text'}
?- 將第一個參數的值插入到占位符中,并使用指定的文本包圍
10、{0:0}
?- 將第一個參數的值按照整數格式進行格式化
11、一些格式化日期的占位符的使用:
{0:D}
?- 將第一個參數的值按照長日期格式進行格式化
DateTime now = DateTime.Now;
string formattedDate = string.Format("Today's date: {0:D}.", now);
Console.WriteLine(formattedDate); //Today's date: Sunday, November 23, 2023.
{0:d}
?- 將第一個參數的值按照短日期格式進行格式化?
DateTime now = DateTime.Now;
string formattedDate = string.Format("Today's date: {0:d}.", now);
Console.WriteLine(formattedDate); //Today's date: 11/23/2023.
- ?
{0:T}
?- 將第一個參數的值按照長時間格式進行格式化
DateTime now = DateTime.Now;
string formattedTime = string.Format("Current time: {0:T}.", now);
Console.WriteLine(formattedTime); //Current time: 9:28:46 PM.
- ?
{0:t}
?- 將第一個參數的值按照短時間格式進行格式化
DateTime now = DateTime.Now;
string formattedTime = string.Format("Current time: {0:t}.", now);
Console.WriteLine(formattedTime); //Current time: 9:28 PM.
{0:F}
?- 將第一個參數的值按照完整日期和時間格式進行格式化
DateTime now = DateTime.Now;
string formattedDateTime = string.Format("Current date and time: {0:F}.", now);
Console.WriteLine(formattedDateTime); //Current date and time: Sunday, November 23, 2023 9:28:46 PM.
{0:M}
?- 將第一個參數的值按照月份和日期格式進行格式化
DateTime now = DateTime.Now;
string formattedDate = string.Format("Today's date: {0:M}.", now);
Console.WriteLine(formattedDate); //Today's date: November 23.
{0:Y}
?- 將第一個參數的值按照年份和月份格式進行格式化
DateTime now = DateTime.Now;
string formattedDate = string.Format("Current month and year: {0:Y}.", now);
Console.WriteLine(formattedDate); //Current month and year: November 2023.
{0:y}
?- 將第一個參數的值按照年份和月份格式進行格式化
DateTime now = DateTime.Now;
string formattedDate = string.Format("Current month and year: {0:y}.", now);
Console.WriteLine(formattedDate); //Current month and year: November 2023.
{0:g}
?- 將第一個參數的值按照一般短日期和時間格式進行格式化
DateTime now = DateTime.Now;
string formattedDateTime = string.Format("Current date and time: {0:g}.", now);
Console.WriteLine(formattedDateTime); //Current date and time: 11/23/2023 9:28 PM.
{0:yyyy-MM-dd}
?,{0:yyyyMMddHHmmss}
- 將第一個參數的值按照指定的日期格式進行格式化
DateTime now = DateTime.Now;
string formattedDate = string.Format("Today's date: {0:yyyy-MM-dd}.", now);//Today's date: 2023-11-23.
string formattedDate1 = string.Format("{0:yyyyMMdd}.", now);//20231123212846
{0:HH:mm:ss}
?- 將第一個參數的值按照指定的時間格式進行格式化
DateTime now = DateTime.Now;
string formattedTime = string.Format("Current time: {0:HH:mm:ss}.", now);
Console.WriteLine(formattedTime); //Current time: 21:28:46.
{0:dddd, MMMM dd, yyyy}
?- 將第一個參數的值按照指定的完整日期格式進行格式化
DateTime now = DateTime.Now;
string formattedDate = string.Format("Today's date: {0:dddd, MMMM dd, yyyy}.", now);
Console.WriteLine(formattedDate); //Today's date: Sunday, November 23, 2023.
{0:hh:mm tt}
?- 將第一個參數的值按照指定的12小時制時間格式進行格式化
DateTime now = DateTime.Now;
string formattedTime = string.Format("Current time: {0:hh:mm tt}.", now);
Console.WriteLine(formattedTime); //Current time: 09:28 PM.
- ?
{0:yyyy-MM-dd HH:mm:ss}
?- 將第一個參數的值按照指定的日期和時間格式進行格式化
DateTime now = DateTime.Now;
string formattedDateTime = string.Format("Current date and time: {0:yyyy-MM-dd HH:mm:ss}.", now);
Console.WriteLine(formattedDateTime); //Current date and time: 2023-11-23 21:28:46.