首頁  >  文章  >  後端開發  >  C# 字串PadLeft

C# 字串PadLeft

PHPz
PHPz原創
2024-09-03 15:16:59295瀏覽

填充只不過是在字串的開頭或結尾插入空格或任何 Unicode 字元。在字串開頭插入空格或 Unicode 字元稱為從左側填充字串。 C# 提供了一個名為 PadLeft() 的方法,可以用來實現此目的。

String 類別包含兩種重載形式的 PadLeft() 方法:

  • String.PadLeft(Int32, Char)
  • String.PadLeft(Int32)

由於字串本質上是不可變的,PadLeft() 方法在向其左側添加指定字元後傳回一個新字串,而不是向現有字串添加字元。

文法:

String.PadLeft() 方法的兩種重載形式的語法如下:

public string PadLeft(int totalLength, char paddingChar);

說明:

上述語法中的 PadLeft() 方法採用兩個參數;第一個是一個整數,指定在原始字串左側新增指定字元後此方法將傳回的字串的長度。第二個參數用於指定用於填充的 Unicode 字元。

public string PadLeft(int totalLength);

說明:

上述語法中的 PadLeft() 方法僅接受一個參數,該參數是一個整數,用於指定在原始字串左側添加空格後結果字串的長度。 PadLeft() 方法的上述兩種重載形式都會傳回字串值,其長度將等於參數中指定的長度。

C# 中 String PadLeft() 方法如何運作?

在 C# 中,「System」命名空間包含一個名為 String 的類,該類別用於處理字串操作,並提供一系列方法來對字串執行不同的操作。其中一種方法是 String.PadLeft() 方法。此方法用於在字串的開頭(即左側)添加指定的字符,然後返回指定長度的新字串。

因此,String.PadLeft() 方法將字串向右移動。

範例:

string str = "Hello";
string resultedString = str.PadLeft(8, '@');

現在讓我們試著借助上面的範例來理解左填充的概念。在這裡,我們透過將結果字串的總長度傳遞為“8”和一個填充字元(即“@”)來對字串(str)應用左填充。這裡,原始字串(即「Hello」)的長度為 5,而我們需要結果字串的長度為 8。因此,結果字串的左側將添加三個“@”字符,從而形成總長度(結果字串的長度)。原始字串加上結果字串中的一些填充字元)等於使用其整數參數傳遞給方法的長度。

如果使用者未在方法中指定任何 Unicode 填充字符,則預設會在原始字串的左側添加空格,其方式與添加 Unicode 填充字符(如果指定)相同。現在,如果使用者指定結果字串的總長度小於原始字串的長度,則該方法將傳回對現有實例的參考。

同樣,如果使用者指定結果字串的總長度等於原始字串的長度,則該方法將傳回與現有字串相同的新字串。由於字串本質上是不可變的,因此 PadLeft() 方法的兩種重載形式都會在將指定字元填入原始字串的左側後傳回一個新字串。如果指定的總長度小於零,String.PadLeft() 方法將傳回 ArgumentOutOfRangeException。

實作 C# 字串 PadLeft 方法的範例

以下是 C# 字串 PadLeft 方法的範例:

範例#1

顯示 PadLeft() 方法基本功能的範例。

代碼:

using System;
using System.Text;
namespace ConsoleApp4
{
public class Program
{
public static void Main(string[] args)
{
string str = "Hello World!";
try
{
//making the length of the string 15
//by adding 3 '@' characters at the beginning of the string
string resultedStringWithChar = str.PadLeft(15, '@');
Console.WriteLine(resultedStringWithChar);
//making the length of the string 15
//by adding 3 white spaces at the beginning of the string
string resultedStringWithoutChar = str.PadLeft(15);
Console.WriteLine(resultedStringWithoutChar);
Console.ReadLine();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}

輸出:

C# 字串PadLeft

我們可以在輸出中看到,為了使結果字串的長度達到 15,結果字串中加入了三個「@」字元。同樣,當我們第二次使用 String.PadLeft() 方法時,我們沒有指定任何字元。因此,結果字串中新增了三個空格。

範例#2

範例顯示所需結果字串的總長度小於或等於原始字串的長度且總長度小於零的情況。

代碼:

using System;
using System.Text;
namespace ConsoleApp4
{
public class Program
{
public static void Main(string[] args)
{
string str = "Hello World!";
try
{
//providing total length as 12
//which is equal to the length of the original string
string resultedStringWithChar = str.PadLeft(12, '@');
Console.WriteLine(resultedStringWithChar);
//providing total length as 10
//which is less than the length of the original string
string resultedStringWithoutChar = str.PadLeft(10);
Console.WriteLine(resultedStringWithoutChar);
resultedStringWithoutChar = str.PadLeft(-1);
Console.WriteLine(resultedStringWithoutChar);
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
}
}

輸出:

C# 字串PadLeft

範例 #3

對陣列中的多個字串套用 PadLeft() 方法的範例。

代碼:

using System;
using System.Text;
namespace ConsoleApp4
{
public class Program
{
public static void Main(string[] args)
{
string[] strArray = { "Lily", "Rose", "Jasmine", "Sunflower" };
char paddingChar = '#';
try
{
//accessing each string of the array
//using 'foreach' loop
foreach (string str in strArray)
{
//adding '#' at the start of each string
Console.WriteLine(str.PadLeft(10, paddingChar));
//using PadLeft() method without specifying
//any padding character
Console.WriteLine(str.PadLeft(10));
}
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
}
}

輸出:

C# 字串PadLeft

結論

在C#中,String.PadLeft()方法用於在字串的開頭或左側添加指定的字元或空格,以達到所需的字串長度。此方法存在於“System”命名空間下,並且有兩種重載形式。

以上是C# 字串PadLeft的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:C# 已檢查下一篇:C# 已檢查