慚愧這麼好用的字串處理功能,看完黑大介紹才知道。
Interpolated String使用方式:
在雙引號前加上 $ 符號,雙引號和 $ 符號之間無空格即可使用。
官方結構示意:
{<interpolationExpression>[,<alignment>][:<formatString>]}| Element | Description 描述 | 
|---|---|
interpolationExpression | The expression that produces a result to be formatted. String representation of null is String.Empty.插入變數的位置  | 
alignment | The constant expression whose value defines the minimum number of characters in the string representation of the expression result. If positive, the string representation is right-aligned; if negative, it's left-aligned. For more information, see Alignment Component. 可省略,字串對齊方式  | 
formatString | A format string that is supported by the type of the expression result. For more information, see Format String Component. 可省略,字串格式  | 
官方範例:
Console.WriteLine($"|{"Left",-7}|{"Right",7}|");
//置左共7格,|Left    |  置右共7格,|  Right|
//          |Left4567|            |76Right|          
const int FieldWidthRightAligned = 20;
Console.WriteLine($"{Math.PI,FieldWidthRightAligned} - default formatting of the pi number");
//F3,浮點數到小數第三位
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參考資料:
$ - string interpolation (C# reference)
C# Interpolated Strings 字串插值
嘗試以自己的角度來整理並紀錄,也許會對一些人有幫助。
文章有錯、參考聯結有漏或是連結失效..等,還請幫忙告知,謝謝。
另外參考資料中有很多更棒的文章,建議多看看。