C# PadLeft() 方法用于获取一个新字符串,如果字符串长度小于指定长度,则该字符串中的字符右对齐。
例如,假设您有“hello C#”作为具有 8 个字符长度的字符串,并且您为 padleft 传递了 10,它将在两个空格之后将字符串移动到右侧。这意味着 PadLeft() 方法为指定长度的字符串提供填充。它用于格式化字符串内容。
public string PadLeft(Int32 length) public string PadLeft(Int32, Char)
长度:它是一个整数类型参数,用于传递填充。
它返回字符串。
using System; public class StringExample { public static void Main(string[] args) { string s1 = "Hello C#";// 8 length of characters string s2 = s1.PadLeft(10);//(10-8=2) adds 2 whitespaces at the left side Console.WriteLine(s2); } }
输出:
Hello C#