C# String PadLeft()
C# String PadLeft()
C# PadLeft() 方法用于获取一个新字符串,如果字符串长度小于指定长度。
例如,假设您将"hello C#"作为具有 8 个字符长度的字符串,并且您为 padleft 传递了 10 个字符,它将在右侧的两个空格之后移动字符串。这意味着 PadLeft() 方法为指定长度的字符串提供填充。用于格式化字符串内容。
签名
public string PadLeft(Int32 length)
public string PadLeft(Int32, Char)
参数
长度: 是一个整型参数,用于传递padding。
返回
返回字符串。
C# String PadLeft() 方法示例
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);
}
}
输出: