【C#】[带格式的字符串] 复合格式设置字符串与使用 $ 的字符串内插 | 如何格式化输出字符串

复合格式输出

string name = "Fred";
String.Format("Name = {0}, hours = {1:hh}", name, DateTime.Now);

通过指定相同的参数说明符,多个格式项可以引用对象列表中的同一个元素。 例如,通过指定“0x{0:X} {0:E} {0:N}”等复合格式字符串,可以将同一个数值设置为十六进制、科学记数法和数字格式,如下面的示例所示:

string multiple = String.Format("0x{0:X} {0:E} {0:N}",
                                Int64.MaxValue);
Console.WriteLine(multiple);
// The example displays the following output:
//      0x7FFFFFFFFFFFFFFF 9.223372E+018 9,223,372,036,854,775,807.00

D 或 d 十进制数

Console.Write("{0:D5}", 25);   //00025  

E 或 e 科学型

Console.Write("{0:E}", 250000);   //2.500000E+005  

F 或 f float

Console.Write("{0:F2}", 25);   //25.00  
Console.Write("{0:F0}", 25);   //25  

N 或 n 数字

Console.Write("{0:N}", 2500000);   //2,500,000.00  

X 或 x 十六进制

Console.Write("{0:X}", 250);  

可以用来显示时间,比如:

int hour = 7;
int minutes = 59;
int seconds = 3;

string timeString = string.Format("{0:00}:{1:00}:{2:00}", hour, minutes, seconds);
Debug.Log(timeString); // 输出:07:59:03

内插字符串$

$ 特殊字符将字符串文本标识为内插字符串 。 内插字符串是可能包含内插表达式的字符串文本 。 将内插字符串解析为结果字符串时,带有内插表达式的项会替换为表达式结果的字符串表示形式。

string name = "Mark";
var date = DateTime.Now;

// 复合格式:
Console.WriteLine("Hello, {0}! Today is {1}, it's {2:HH:mm} now.", name, date.DayOfWeek, date);
// 字符串内插:
Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");
// 两者输出完全相同,如下:
// Hello, Mark! Today is Wednesday, it's 19:40 now.

可以用可选格式

{<interpolationExpression>[,<alignment>][:<formatString>]}

Console.WriteLine($"|{"Left",-7}|{"Right",7}|");

const int FieldWidthRightAligned = 20;
Console.WriteLine($"{Math.PI,FieldWidthRightAligned} - default formatting of the pi number");
Console.WriteLine($"{Math.PI,FieldWidthRightAligned:F3} - display only three decimal digits of the pi number");
// Expected output is:
// |Left   |  Right|
//     3.14159265358979 - default formatting of the pi number
//                3.142 - display only three decimal digits of the pi number

也支持原始字符串(三个引号)与表达式( 如 {Math.Sqrt(X * X + Y * Y)} )
字符串字面量可以包含任意文本,而无需转义序列。 字符串字面量可以包括空格和新行、嵌入引号以及其他特殊字符。

int X = 2;
int Y = 3;

var pointMessage = $"""The point "{X}, {Y}" is {Math.Sqrt(X * X + Y * Y)} from the origin""";

Console.WriteLine(pointMessage);
// output:  The point "2, 3" is 3.605551275463989 from the origin.

Unity

所有使用string类型的参数的地方均可使用,如可用于debug.log内:

string path = Application.persistentDataPath + "DataSave.json";
Debug.Log($"File not exist : {path}");

在这里插入图片描述

参考

复合:
https://learn.microsoft.com/zh-cn/dotnet/standard/base-types/composite-formatting
内插:
https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/tokens/interpolated
原始字符串字面量:
https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/reference-types#string-literals

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值