C# 補空白字元

  • 3459
  • 0

C# 補空白字元

PadRight:

PadRight(Int32) 傳回新字串,此字串會以空格填補右側至指定的總長度,靠左對齊這個字串中的字元。
PadRight(Int32, Char) 傳回新字串,此字串會以指定的 Unicode 字元填補右側至指定的總長度,靠左對齊這個字串中的字元。
string str = "ABC";
char c = '0';
str = str.PadRight(10,c);
Console.WriteLine(str);

輸出: ABC0000000

PadLeft:

PadLeft(Int32) 傳回新字串,此字串會以空格填補左側至指定的總長度,靠右對齊這個執行個體中的字元。
PadLeft(Int32, Char) 傳回新字串,此字串會以指定的 Unicode 字元填補左側至指定的總長度,靠右對齊這個執行個體中的字元。
string str = "ABC";
char c = '0';
str = str.PadLeft(10,c);
Console.WriteLine(str);

輸出: 0000000ABC

參考:

https://docs.microsoft.com/zh-tw/dotnet/api/system.string.padright?view=net-5.0