當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C# String PadLeft()用法及代碼示例

C# PadLeft() 方法用於獲取一個新的字符串,如果字符串長度小於指定長度,則 right-aligns 該字符串中的字符。

例如,假設您將“hello C#”作為具有 8 個字符長度的字符串,並且您為 padleft 傳遞了 10 個字符,它會在兩個空格後移動右側的字符串。這意味著 PadLeft() 方法為指定長度的字符串提供填充。它用於格式化字符串內容。

簽名

public string PadLeft(Int32 length)
public string PadLeft(Int32, Char)

參數

length:它是一個整數類型參數,用於傳遞填充。

返回

它返回字符串。

C# 字符串 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);
      }  
    }

輸出:

  Hello C#




相關用法


注:本文由純淨天空篩選整理自 C# String PadLeft()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。