C# String 字串處理
2021-06-27
筆記 C# 字串處理的相關知識。
C# String
字串插值 (String Interpolation) (C#6)
string name = "Mark";
var date = DateTime.Now;
// Composite formatting:
Console.WriteLine("Hello, {0}! Today is {1}, it's {2:HH:mm} now.", name, date.DayOfWeek, date);
// String interpolation:
Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");
逐字識別項
可以讓 Escape Char 不發揮作用。
var normalString = "Hello\r\nWorld";
// Hello
// World
var verbatimString = @"Hello\r\nWorld";
// Hello\r\nWorld
字串操作 - 數字自動補零 (Padding String with 0)
var number = 5;
number.toString().PadLeft(2, '0');
// 05
var paragraph = "abc";
number.toString().PadLeft(10);
// ' abc'