使用 PadLeft() 方法用空格填充字符串的開頭。您也可以使用 Unicode 字符填充它。
假設以下是我們的字符串。
string myStr = "DemoOne";
要在上述字符串的開頭設置填充,請使用 PadLeft 方法。
myStr.PadLeft(10);
這是完整的示例。
示例
using System;
class Demo {
static void Main() {
string myStr = "DemoOne";
// set padding at the beginning
Console.WriteLine(myStr.PadLeft(10));
myStr = "DemoTWO";
// set padding at the beginning
Console.Write(myStr.PadLeft(15));
}
}
輸出
DemoOne DemoTWO
相關用法
- C# String.ToUpperInvariant用法及代碼示例
- C# String.ToLowerInvariant用法及代碼示例
- C# String.IsNullOrEmpty()用法及代碼示例
- C# String.ToCharArray()用法及代碼示例
- C# String.Copy()用法及代碼示例
- C# String.Clone()用法及代碼示例
- C# String.Insert()用法及代碼示例
- C# String.Split()用法及代碼示例
- C# String.Contains()用法及代碼示例
- C# String.Format()函數用法及代碼示例
- C# String.CopyTo()用法及代碼示例
- C# String.Compare()用法及代碼示例
- C# String.IndexOf()用法及代碼示例
- C# String.IsNormalized用法及代碼示例
- C# String.Format()方法用法及代碼示例
- C# String.IndexOf()方法用法及代碼示例
- C# StringBuilder.ToString用法及代碼示例
- C# String ToString()用法及代碼示例
- C# String Contains()用法及代碼示例
注:本文由純淨天空篩選整理自Samual Sam大神的英文原創作品 C# String.PadLeft Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。