C# 字串格式()

王林
發布: 2024-09-03 15:15:46
原創
801 人瀏覽過

可以使用 C# 中稱為 string Format 方法的方法將變數、表達式或物件的值插入到另一個字串中。透過使用字串格式方法,指定字串的格式項目將替換為指定物件的字串表示形式。字串格式方法有多種類型,例如日期時間格式方法、數字格式方法、自訂格式方法等。透過使用這些不同類型的格式方法,可以將格式項目替換為 C# 程式語言中物件的相應表示形式.

文法:

C# String Format 方法的語法如下:

public string Format(string, object)
public string Format(string, object, object)
public string Format(IFormatProvider, string, object)
登入後複製

其中format方法的第一個語法用於將指定字串的格式項目替換為指定物件的字串表示形式。 format 方法的第二種語法用於將指定字串的格式項目替換為兩個指定物件的字串表示形式。 format 方法的第三個語法用於將指定字串的格式項目替換為對應物件的字串表示形式。

C#字串格式化方法的功能

  • 每當需要透過將字串替換為不同物件的字串表示形式來格式化字串時,我們都會使用字串格式方法。
  • 透過使用字串格式化方法,可以將指定字串的格式項目替換為指定物件的字串表示形式。
  • 透過使用字串格式化方法,可以將指定字串的格式項目替換為兩個指定物件的字串表示形式。
  • 透過使用字串格式化方法,可以將指定字串的格式項目替換為對應物件的字串表示形式。
  • 有多種格式可以指定字串的格式項目。有數字格式、日期和時間格式以及自訂格式。

C# 字串格式範例()

以下是範例:

範例#1

C#程式示範字串格​​式化方法,用兩個以上物件取代指定字串的格式項目:

代碼:

using System;
//a namespace called program is defined
namespace program
{
//a class called check is defined
class check
{
//main method is called
static void Main(string[] args)
{
//a string variable is used to store the format items that needs to be replaced with the string representation of objects
string str = "{0} {1:0.0%}";
//string format method is used to replace the format items of the specified string with the string representation of objects
string res = string.Format(str, "India has a total power consumption of", 0.73);
Console.WriteLine("The statement after using the string format method is:");
Console.WriteLine("\n {0}",res);
Console.ReadLine();
}
}
}
登入後複製

輸出:

C# 字串格式()

在上面的程式中,建立了一個名為program的命名空間。然後建立一個名為 check 的類,在該類別中呼叫 main 方法。在 main 方法內部,定義了一個字串變數來儲存需要用物件的字串表示形式替換的格式項目。其中一個格式字串以 % 符號指定,這表示它將給定值乘以 100 並給出結果。因此,從輸出可以看出,當格式項為 0.0% 時,我們獲得了 73.0%。然後使用字串格式方法將字串的格式項目替換為指定物件的字串表示形式。

範例#2

C#程式示範字串格​​式化方法,將指定整數值的格式項目替換為十六進位表示,並使用DateTime.Now屬性顯示日期和時間格式:

代碼:

using System;
//a namespace called program is defined
namespace program
{
//a class called check is defined
class check
{
//main method is called
static void Main(string[] args)
{
//an integer variable is used to store the value
int value = 200;
//hexadecimal format method is used to replace the format items of the specified integer value with the hexadecimal representation of objects
Console.WriteLine("The statement after using the hex format method is {0:x}", value);
//DateTime.Now is used to obtain the current date and time by creating an instance of it
DateTimedt = DateTime.Now;
Console.WriteLine("The current date and time is: {0}", dt);
//By using date format which can display only the date, the current date is displayed
Console.WriteLine("The current date is: {0:D}", dt);
//By using time format which can display only the time, the current time is displayed
Console.WriteLine("The current time is: {0:T}", dt);
//a string variable is used to store the values for padding, here negative values indicate left alignment and positive values indicate right alignment
string hey = "{0,-40} {0,40}";
string res = string.Format(hey,"This is describing padding");
Console.WriteLine(res);
Console.ReadLine();
}
}
}
登入後複製

輸出:

C# 字串格式()

在上面的程式中,建立了一個名為program的命名空間。然後建立一個名為 check 的類,在該類別中呼叫 main 方法。在main方法中,定義了一個整數變數來儲存需要轉換為十六進位格式的整數。然後使用十六進位格式方法將格式項目替換為物件的十六進位表示形式。然後使用 DateTime.Now 透過建立它的實例來取得目前日期和時間。然後使用只能顯示日期的日期格式,顯示目前日期。然後使用只能顯示時間的時間格式,顯示當前時間。然後使用字串變數來儲存填充值,其中負值表示左對齊,正值表示右對齊。輸出如上面的快照所示。

以上是C# 字串格式()的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!