C#函數,PadLeft(),填充指定數量的空格。

來源:互聯網
上載者:User
返回一個新字串,該字串通過在此執行個體中的字元左側填充空格來達到指定的總長度,從而實現靠右對齊。

 

命名空間:  System
程式集:  mscorlib(在 mscorlib.dll 中) public string PadLeft(
int totalWidth
)參數

totalWidth
類型:System.Int32
結果字串中的字元數,等於原始字元數加上任何其他填充字元。
傳回值

類型:System.String
與此執行個體等效的一個新字串,但該字串為靠右對齊,因此,在左側填充所需數量的空格,使長度達到 totalWidth。 如果 totalWidth 小於此執行個體的長度,則為與此執行個體相同的新字串。
異常

異常 條件
ArgumentOutOfRangeException

totalWidth 小於零。

備忘

Unicode 空格定義為十六進位的 0x20。

PadLeft(Int32) 方法填充返回字串的開頭。 這意味著當對從右向左的語言使用該方法時,它將填充字串的右部。

注意

此方法不修改當前執行個體的值。 而是返回一個新字串,其中填充了前置空白以使字串的總長度為 totalWidth 個字元。

樣本

下面的樣本說明 PadLeft 方法。

C#
string str = "BBQ and Slaw";Console.WriteLine(str.PadLeft(15));  // Displays "   BBQ and Slaw".Console.WriteLine(str.PadLeft(5));   // Displays "BBQ and Slaw".

轉自MSDN:http://msdn.microsoft.com/zh-cn/library/0zk6ydzx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.