C# String PadLeft

PHPz
PHPzOriginal
2024-09-03 15:16:59579browse

Padding is nothing but inserting a white space or any Unicode character at the beginning or at the end of a string. Inserting white space or Unicode character at the beginning of a string is called padding the string from its left. C# provides a method called PadLeft() which can be used to achieve this.

String class contains two overloaded forms of PadLeft() method:

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

As strings are immutable in nature PadLeft() method returns a new string after adding the specified character to its left instead of adding characters to the existing string.

Syntax:

The syntax of String.PadLeft() method in both its overloaded forms is as follows:

public string PadLeft(int totalLength, char paddingChar);

Explanation:

The PadLeft() method in the above syntax takes two arguments; the first is an integer which specifies the length of the string which will be returned by this method after adding the specified character to the left of the original string. The second parameter is used to specify a Unicode character which will be used for padding.

public string PadLeft(int totalLength);

Explanation:

The PadLeft() method in the above syntax takes only one argument which is an integer used to specify the length of the resulted string after adding spaces to the left of the original string. Both the above-overloaded forms of PadLeft() method returns a string value whose length will be equal to the length specified in the argument.

How does String PadLeft() Method work in C#?

In C#, the ‘System’ namespace contains a class called String which is used to handle string manipulations and provides a range of methods to perform different operations on a string. One such method is String.PadLeft() method. This method is used to add a specified character to the beginning i.e to the left of the string and then returns a new string of the specified length.

Thus, String.PadLeft() method shifts the string to the right.

Example:

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

Let us now try to understand the concept of left padding with the help of the above example. Here, we applied left padding to string (str) by passing the total length of the resulted string as ‘8’ and a padding character i.e. ‘@’. Here, the length of the original string i.e. “Hello” is 5 and we need the length of the resulted string as 8. Thus, the resulted string will have three ‘@’ characters added to its left making the total length (length of the original string plus a number of padding characters in the resulted string) equal to the length passed to the method using its integer argument.

If the user has not specified any Unicode padding character in the method then by default space will be added to the left of the original string in the same way as a Unicode padding character would have been added if specified. Now, if a user specifies the total length of the resulted string which is less than the length of the original string then the method will return a reference to the existing instance.

In the same way, if a user specifies the total length of the resulted string which is equal to the length of the original string then the method will return a new string which will be identical to the existing string. As strings are immutable in nature both the overloaded forms of PadLeft() method returns a new string after padding the specified character to the left of the original string. String.PadLeft() method returns ArgumentOutOfRangeException if the total length specified is less than zero.

Examples to Implement C# String PadLeft Method

Below are the examples of C# String PadLeft Method:

Example #1

Example showing basic functionality of the PadLeft() method.

Code:

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);
}
}
}
}

Output:

C# String PadLeft

We can see in the output that to achieve the length of the resulted string as 15, three ‘@’ characters are added to the resulted string. In the same way, when we used the String.PadLeft() method for the second time, we did not specify any character. Thus, three white spaces have been added to the resulted string.

Example #2

Example showing a scenario when the total length of the required resulted string is less than or equal to the length of the original string and the total length is less than zero.

Code:

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();
}
}
}
}

Output:

C# String PadLeft

Example #3

Example applying PadLeft() method on multiple strings from an array.

Code:

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();
}
}
}
}

Output:

C# String PadLeft

結論

C# では、String.PadLeft() メソッドを使用して、指定した文字または空白を文字列の先頭に追加するか、文字列の左側に追加して、必要な文字列の長さを実現します。このメソッドは「System」名前空間の下に存在し、2 つのオーバーロードされた形式を持ちます。

The above is the detailed content of C# String PadLeft. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C# checkedNext article:C# checked