Composite format output
string name = "Fred";
String.Format("Name = {0}, hours = {1:hh}", name, DateTime.Now);
Multiple format items can refer to the same element in the object list by specifying the same argument specifier. For example, by specifying a compound format string such as "0x{0:X} {0:E} {0:N}", the same value can be formatted in hexadecimal, scientific notation, and number, as follows The example shows:
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 or d decimal number
Console.Write("{0:D5}", 25); //00025
E or e scientific
Console.Write("{0:E}", 250000); //2.500000E+005
F or f float
Console.Write("{0:F2}", 25); //25.00
Console.Write("{0:F0}", 25); //25
N or n numbers
Console.Write("{0:N}", 2500000); //2,500,000.00
X or x hexadecimal
Console.Write("{0:X}", 250);
Can be used to display time, for example:
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
interpolated string $
The $ special character identifies a string literal as an interpolated string. Interpolated strings are string literals that may contain interpolated expressions. When parsing an interpolated string into a result string, items with interpolated expressions are replaced with the string representation of the result of the expression.
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.
optional format
{
<interpolationExpression>[,<alignment>][:<formatString>]}
like
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
Raw strings (three quotes) and expressions (eg {Math.Sqrt(X * X + Y * Y)} ) are also supported.
String literals can contain arbitrary text without escape sequences. String literals can include spaces and newlines, embedded quotes, and other special characters.
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
All places that use string type parameters can be used, such as in debug.log:
string path = Application.persistentDataPath + "DataSave.json";
Debug.Log($"File not exist : {
path}");
reference
Composite:
https://learn.microsoft.com/en-us/dotnet/standard/base-types/composite-formatting
Interpolation:
https://learn.microsoft.com/en-us/dotnet/csharp/language- reference/tokens/interpolated
raw string literals:
https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/reference-types#string-literals