庐江县建设网站,影视广告宣传片制作公司,餐饮vi设计公司,钢铁网站哪家做的好复合格式输出
string name Fred;
String.Format(Name {0}, hours {1:hh}, name, DateTime.Now);通过指定相同的参数说明符#xff0c;多个格式项可以引用对象列表中的同一个元素。 例如#xff0c;通过指定“0x{0:X} {0:E} {0:N}”等复合格式字符…复合格式输出
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.223372E018 9,223,372,036,854,775,807.00D 或 d 十进制数
Console.Write({0:D5}, 25); //00025 E 或 e 科学型
Console.Write({0:E}, 250000); //2.500000E005 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); 内插字符串$
$ 特殊字符将字符串文本标识为内插字符串 。 内插字符串是可能包含内插表达式的字符串文本 。 将内插字符串解析为结果字符串时带有内插表达式的项会替换为表达式结果的字符串表示形式。
string name Mark;
var date DateTime.Now;// 复合格式:
Console.WriteLine(Hello, {0}! Today is {1}, its {2:HH:mm} now., name, date.DayOfWeek, date);
// 字符串内插:
Console.WriteLine($Hello, {name}! Today is {date.DayOfWeek}, its {date:HH:mm} now.);
// 两者输出完全相同如下:
// Hello, Mark! Today is Wednesday, its 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