C#string.Format的一些使用

WPF中StringFormat的用法 WPF中StringFormat的用法可以参照C#中string.Format的用法 立即下载
AI摘要

本文详细介绍了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.

C#中string.format用法详解 主要介绍了C#中string.format用法,以实例形式较为详细的讲述了string.format格式化的各种用法,非常具有实用价值,需要的朋友可以参考下 立即下载

相关推荐

数据集分享 | IWR1642呼吸心跳数据集

雷达开源数据库 数据集

【调皮连续波】雷达技术领域知识博主 3216

C# 之 String.Format详解

有兴趣的同学可以去看官方文档String.Format Method (System) | Microsoft Docs 本文仅对官方文档中的内容进行提炼和总结 功能描述:把一些对象(Object)的值转化为字符串,并按照formats里的格式塞到另外一个字符串里 函数重载: public static string Format(IFormatProvider provider, string format, object arg0) public static string Forma..

zhaocg00的专栏 2万+

Dell PowerEdge T630加装RTX显卡保姆级指南:从1100W电源模块到GPU供电卡,一个都不能少

本文详细介绍了在Dell PowerEdge T630服务器上加装RTX显卡的完整流程,涵盖硬件兼容性验证、1100W电源模块升级、GPU供电卡安装及散热优化等关键步骤。通过实战案例和专业技术指导,帮助用户克服服务器架构限制,实现高性能GPU的稳定运行,适用于AI训练、3D渲染等高负载场景。

weixin_30664615的博客 378

C# string.Format

转载 string.Format对C#字符串格式化 String.Format 方法的几种定义: String.Format (String, Object) 将指定的 String 中的格式项替换为指定的 Object 实例的值的文本等效项。String.Format (String, Object[]) 将指定 String 中的格式项替换为指定数组中相应 Object 实例的值的文本等效项。String.Format (IFormatProvider, String, ...

weixin_53370274的博客 8851

AEB(自动紧急制动)

AEB系统对静止物体的误判率受多种因素影响,包括传感器精度、算法复杂性以及环境条件等。通过多传感器融合、算法优化、硬件升级以及环境适应性增强等技术手段,可以有效减少误判问题。美国和欧洲的AEB法规均强调了技术性能和安全性,但具体要求有所不同。美国的NHTSA规定更注重高速和复杂场景下的紧急制动能力,而欧盟的ECE R152法规则更侧重于基础的AEB功能覆盖范围。

百态·数智思维 | 聚焦大数据、量化与未来AI 2556

ArcGIS-裁剪遥感影像及dem图像

6、选择【工具箱】-【数据管理工具】-【栅格】-【裁剪】。在【输入栅格】里选择要裁剪的dem,在【输出范围】里选择刚创建的裁剪范围。选择一个输出路径,其他参数保存默认。2、起一个名称,要素类型选择【面】。点击【编辑】选择与要裁剪的dem一致的地理坐标系。1、创建需要裁剪区域的面层shp,右击选择【新建】-【shapefile】3、点击编辑器,选择开始编辑。4、选择【裁剪范围】,点击确定。5、画出想要裁剪的范围并保存。

siwenaiyou的博客 1万+

String.format()的详细用法

String.format() 的用法

星海IT学习之路 7万+

共模电感在EMI滤波中的实战应用与设计技巧

本文深入探讨了共模电感在EMI滤波中的关键作用与设计技巧,涵盖电感值选择、饱和电流风险、磁芯材料性能及绕组工艺等实战经验。通过具体案例解析,展示了共模电感在开关电源、高速接口及汽车电子中的高效应用,帮助工程师解决电磁兼容问题并提升信号完整性。

weixin_27051161的博客 428

C#string.Format

占位符 格式化 结果 描述 string.Format("{0,4}", 0) 0 不满足指定位数的情况下,在前置插入空格 string.Format("{0,-4}", 0) 0 不满足指定位数的情况下,在后置插入空格 数字格式化 格式化 结果 格式符 描述 string.Format("{0:0.0}", 5) 5.0 string.Format("{0:0...

breakbridge的博客 2080

RAD Studio 12安装全攻略:从下载到破解InterBase的完整流程(附KeyPatch使用技巧)

本文提供RAD Studio 12的完整安装指南,包括系统要求、安装步骤及InterBase数据库的高效配置方法。详细介绍了如何使用KeyPatch工具进行许可证处理,并分享了IDE优化和开发技巧,帮助开发者快速上手这一强大的跨平台开发工具。

q6r7s8t9的博客 992

7、CUDA 软件架构与开发工具详解

本文深入探讨了CUDA的软件架构与核心开发工具,涵盖CUDA数组与纹理、表面引用、图形互操作性机制,以及CUDA运行时与驱动程序API的功能对比。详细解析了nvcc编译器的使用方法、编译轨迹与代码生成选项,并结合实际示例说明其应用。文章旨在帮助开发者理解不同技术的选择依据,合理利用CUDA工具链,提升GPU计算性能,适用于从入门到进阶的CUDA开发人员参考。

nnn11的博客 109

C#string.Format小记

//小数点后一位 四舍五入 有 Console.WriteLine("原数据 :12345.46"); Console.WriteLine(string.Format("{0:N1}", 12345.46)); //小数点后两位 四舍五入 有 Console.Writ...

lingwei_liu的博客 197

块存储:AIO的直接写流程注释

提交io流程中aio_write之前函数的注释,可参考“块存储:AIO的直接读流程注释”。

yiyeguzhou100的专栏 1341

详解C#string.Format性能

大家使用String.Format需要注意的一下性能问题。 以前的我总是觉得String.Format用起来非常方便,比+号拼接好多了,久而久之就习惯了用String.Format这种方式去拼接字符串。今天闲来无聊,就具体得了解了一下String.Format。我这里使用的是反编译工具(Reflector),那么现在就一起去探索一下String.Format这个方法吧。 通过反编译工具查看之后

你的骄傲、虽败犹荣 4968

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

C#String.Format大全

字符型转换 转为字符串  12345.ToString("n"); //生成 12,345.00  12345.ToString("C"); //生成 ¥12,345.00  12345.ToString("e"); //生成 1.234500e+004  12345.ToString("f4"); //生成 12345.0000  12345.ToString("x"); //生成 3

鞠勇鸿的技术专栏 1735

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

C#String.Format数字格式化输出

C#:String.Format数字格式化输出     int a = 12345678;   //格式为sring输出//   Label1.Text = string.Format("asdfadsf{0}adsfasdf",a);//   Label2.Text = "asdfadsf"+a.ToString()+"adsfasdf";//   Label1.Text = strin...

hslh23的博客 365

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

c#string.format入门(1)

问:c#中 s += (string.Format("{0}*{1}={2}", i, j, i * j)).PadRight(8);这是什么意思 分解开来 一 string.Format("{0}*{1}={2}", i, j, i * j) string.Format是格式化字符串,{0}代表字符串后边第一个参数,就是i,{1}第二个,{2}第三个 比如i=2,j=3的话,string.

dragoo1的专栏 1088

C#string.format() 格式化数值结果表

C#格式化数值结果表 字符 说明 示例 输出 C 货币 string.Format("{0:C3}", 2) $2.000 D 十进制 string.Format("{0:D3}", 2) 002 E 科学计数法 1.20E+001 1.20E+001 G 常规 string.Format("{0:G}", 2) 2 N 用分号隔开的数字 st...

weixin_34038652的博客 138

c#string倒数第二位插入字符_【转载】 C#使用String.Format拼接字符串

在C#程序开发过程中,很多时候会使用字符串拼接,最简单的字符串拼接操作就是所有的字符串使用加号+相加连接起来,但这种代码形式非常不适合代码维护阅读,尤其是拼接字符串语句比较复杂的时候,如拼接SQL语句等情况,此时就可考虑使用String.Format函数来实现,通过占位符将语句中需要放入变量的位置空出来,而后再用对应的变量值来格式化。例如,我们要拼接一个Sql查询语句,直接拼接的语句如下:stri...

weixin_33371495的博客 190

c#string转浮点数_C#| 使用String.Format()方法使用千位分隔符打印浮点数

c#string转浮点数To print a float number with thousand separators, we can use String.Format() method, here is the example. 要打印带有一千个分隔符的浮点数,我们可以使用String.Format()方法,这里是示例。 using System; namespace Consol...

cumubi7552的博客 1233

C#的String.Format 方法

arg0);参数复合格式字符串。要设置格式的对象。返回Stringformat 的副本,其中的任何格式项均替换为 arg0 的字符串表示形式。例外format 为 null。format 中的格式项无效。或格式项的索引不为零。

wenchm的博客 6305

c# string.Format 传入任意个参数的问题

翻看API,string.Format有以下重载,最适合的是public static String Format(String format, params object[] args);从这个思路来看,我们只需要把任意参数转换成 params object[] 类型参数即可,以下是扩展容器的转换方法.在使用C#开发时遇到string.Format 需要传递任意个参数的情况(一般是配置中不确定参数个数)查遍全网也没有比较合适的解决方法.没办法,只有自己摸索了.

CoolYsl的专栏 885

【MySQL】 DATE_FORMAT() 格式化时间

我们会在项目中遇到这样一种需要,时间存储的时候存的是datetime,也就是年月日,时分秒,但是我们在使用的时候只需要使用年月日的时间,如果我们查询出成千上万条数据,使用代码转化可能会降低效率以及使得代码变得更加繁琐,这时候我们可以考虑在数据源就将其转化。这里介绍一种SQL时间格式化方法 。(参考w3c) DATE_FORMAT() 定义和用法 DATE_FORMAT() 函数用于以不同的格...

ouYang的博客 1万+

C# string Format

使用C#格式化字符串 转载自博客园,原文连接:https://www.cnblogs.com/FlyingBread/archive/2007/01/18/620287.html,向原文作者献上敬意 1 前言 如果你熟悉Microsoft Foundation Classes(MFC)的CString,Windows Template Library(WTL)的CStrin...

gunjiu4462的博客 4080

.net(C#)中String.Format如何使用

导读:   今天写程序时,发现了一个 String 类的用法,感觉以前没有见过,于是到网上找了些资料看了,原来它的这个用法和 Console.write() 有些相似,相信你一看就可以明白.难道不是吗,? 转载的文章内容如下,发现百度知道还真他妈的牛比,什么都有..  String.Format   将指定的 String 中的每个格式项替换为相应对象的值的文本等效项。   例子:   i

十一郎的IT博客 1649
上一篇: 关于Unity Time.deltaTime的理解和使用
下一篇: C#枚举的使用
程序猿游也
博客等级 码龄9年 2291粉丝 273原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿游也

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值