C#string.Format的一些使用

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.

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/164623.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/164623.shtml
英文地址,請注明出處:http://en.pswp.cn/news/164623.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Java常用類

目錄 包裝類 裝箱和拆箱 包裝類型和String的轉換&#xff0c;包裝類的常用方法 包裝類 裝箱和拆箱 package com.edu.wrapper;public class Interger01 {//演示int<-->Integer的裝箱和拆箱//手動裝箱int n1100;Integer integer new Integer(n1);Integer integer01 In…

HCIP---MPLS---LDP

文章目錄 前言一、pandas是什么&#xff1f;二、使用步驟 1.引入庫2.讀入數據總結 前言 MPLS 基于標簽轉發表進行轉發&#xff0c;與路由表類似&#xff0c;標簽轉發表有兩種獲取渠道&#xff1a;一是手動配置(類似靜態路由)&#xff0c;二是通過協議自動學習(類似OSPF)。手動配…

6.1.webrc媒體協商

那今天呢&#xff1f;我們來看一下y8 rtc的媒體協商&#xff0c;那實際上在我們之前的課程中呢&#xff1f;我已經向你介紹過y8 rtc的媒體協商了。只不過呢&#xff0c;角度是不一樣的&#xff0c;在之前介紹外邊tc媒體協商的時候呢&#xff0c;我們是從應用的角度來看。那web …

ActiveMQ消息中間件應用場景

一、ActiveMQ簡介 ActiveMQ是Apache出品&#xff0c;最流行的&#xff0c;能力強勁的開源消息總線。ActiveMQ是一個完全支持JMS1.1和J2EE1.4規范的JMS Provide實現。盡管JMS規范出臺已經是很久的事情了&#xff0c;但是JMS在當今的J2EE應用中仍然扮演這特殊的地位。 二、Active…

《第一行代碼:Android》第三版-2.5.4數據類與單例_數據類

本文主要講述數據類&#xff0c;以前用java實現數據類很麻煩&#xff0c;寫很多代碼&#xff0c;就是搭個數據類的框架。kotlin用data class 關鍵字&#xff0c;給你簡化了數據類的創建&#xff0c;比較貼心。 就是自動為你創建了&#xff1a;equals()、hashCode()、toString(…

深入理解JVM 類加載機制

深入理解JVM 類加載機制 虛擬機如何加載Class文件&#xff1f; Class文件中的信息進入到虛擬機后會發生什么變化&#xff1f; 類加載機制就是Java虛擬機把描述類的數據從Class文件加載到內存&#xff0c;并對數據進行校驗、轉換解析和初始化&#xff0c;最終形成可以被虛擬機…

實現點擊一個選框 使得一個組件的可選性修改

實現效果 代碼 html <div class"divrow"><el-checkbox-group v-model"isSendTag" :max"1"><el-checkbox v-for"(item, index) in isSendTagOptions" :key"index" :label"item.value">{{item.…

【C++設計模式】單例模式singleton

C 設計模式–單例模式singleton 單例模式 單例模式是指確保一個類在任何情況下都絕對只有一個實例&#xff0c;并提供一個全局訪問點。 優點&#xff1a;內存中只有一個實例&#xff0c;減少內存開銷&#xff1b;避免對資源多重占用&#xff1b;設置全局訪問點&#xff0c;嚴…

【Axure教程】用中繼器制作卡片多條件搜索效果

卡片設計通過提供清晰的信息結構、可視化吸引力、易擴展性和強大的交互性&#xff0c;為用戶界面設計帶來了許多優勢&#xff0c;使得用戶能夠更輕松地瀏覽、理解和互動。 那今天就教大家如何用中繼器制作卡片的模板&#xff0c;以及完成多條件搜索的效果&#xff0c;我們會以…

并發與并行

并發和并行是操作系統中的兩個重要概念&#xff0c;它們在定義和處理任務的方式上有一些區別。 并發&#xff08;concurrency&#xff09;是指在一段時間內&#xff0c;有多個程序都處于啟動運行到運行完畢之間&#xff0c;但任一時刻點上只有一個程序在處理機上運行。它是一種…

Vue偵聽器

Vue偵聽器是根據組件狀態做DOM更新或者異步更新其他級聯狀態的。計算屬性的主要目標是根據已有數據計算出組件的狀態&#xff0c;它是組件內部的計算&#xff0c;計算結果在組件內部應用。偵聽器的主要目標是根據組件狀態的變動&#xff0c;做級聯的或者異步的操作或DOM更新&am…

STM32F103C8T6第6天:adc、iic、spi、溫濕度dht11在lcd1602顯示

1. ADC介紹 ADC是什么&#xff1f; Analog-to-Digital Converter&#xff0c;指模擬/數字轉換器 ADC的性能指標 量程&#xff1a;能測量的電壓范圍分辨率&#xff1a;ADC能辨別的最小模擬量&#xff0c;通常以輸出二進制數的位數表示&#xff0c;比如&#xff1a;8、10、1…

【Spring篇】Spring注解式開發

本文根據嗶哩嗶哩課程內容結合自己自學所得&#xff0c;用于自己復習&#xff0c;如有錯誤歡迎指正&#xff1b; 我在想用一句話激勵我自己努力學習&#xff0c;卻想不出來什么驚為天人、精妙絕倫的句子&#xff0c;腦子里全是上課老師想說卻沒想起的四個字 “ 唯手熟爾 ”&am…

自動駕駛術語匯總

目錄 智駕級別芯片相關自動駕駛相關輔助駕駛相關預警相關傳感器相關泊車相關安全相關車燈相關 智駕級別 L0-L2屬于輔助駕駛&#xff0c;L4-L5才算自動駕駛 L0&#xff08;Level 0&#xff09;&#xff1a;無自動化。這是大多數傳統汽車的級別&#xff0c;所有的駕駛任務都需要…

C++每日選擇題—Day1

第一題 以下C代碼會輸出什么? #include <iostream> using namespace std; class A { public:A() {}~A() {} private:static int a; }; int main() {cout << sizeof(A) << endl;return 0; } A&#xff1a;0 B&#xff1a;1 C&#xff1a;4 D&#xff1a;8 答…

2023年最新PyCharm環境搭建教程(含Python下載安裝)

文章目錄 寫在前面PythonPython簡介Python生態圈Python下載安裝 PyCharmPyCharm簡介PyCharm下載安裝PyCharm環境搭建 寫在后面 寫在前面 最近博主收到了好多小伙伴的吐槽稱不會下載安裝python&#xff0c;博主聽到后非常的扎心&#xff0c;經過博主幾天的熬夜加班&#xff0c;…

單鏈表實現【隊列】

目錄 隊列的概念及其結構 隊列的實現 數組隊列 鏈式隊列 隊列的常見接口的實現 主函數Test.c 頭文件&函數聲明Queue.h 頭文件 函數聲明 函數實現Queue.c 初始化QueueInit 創建節點Createnode 空間釋放QueueDestroy 入隊列QueuePush 出隊列QueuePop 隊頭元…

Hyper-V系列:Hyper-V啟動、創建虛擬機、與主機傳輸文件

Hyper-V啟動、創建虛擬機、與主機傳輸文件 一. 簡介二. 啟用Hyper-V的方式也很簡單:一、從“任務管理器”的“性能”查看虛擬化是否啟用,未啟用的需要到BIOS開啟:右下角可以看到“虛擬化:已啟用”二、啟用Hyper-v和虛擬機1.電腦左下角右鍵打開應用界面——可選功能2.在可選…

JavaScript 原始數據類型和對應的對象類型(內置對象)之間的關系

JavaScript 原始數據類型和對應的對象類型&#xff08;內置對象&#xff09;之間的關系 JavaScript 的原始&#xff08;primitive&#xff09;數據類型包括包括數字&#xff08;Number&#xff09;、字符串&#xff08;String&#xff09;、布爾值&#xff08;Boolean&#xf…

【數據結構】E : 貨幣套匯(圖路徑)

E : 貨幣套匯&#xff08;圖路徑&#xff09; Description 套匯是指利用貨幣匯兌率的差異將一個單位的某種貨幣轉換為大于一個單位的同種貨幣。例如&#xff0c;假定1 美元可以買0.7 英鎊&#xff0c;1 英鎊可以買9.5 法郎&#xff0c;1法郎可以買到0.16美元。通過貨幣兌換&a…