C# String Padding
In C#, you can pad strings to ensure they have a certain length by using the PadLeft()
or PadRight()
methods. These methods add spaces or other characters to the beginning or end of the string.
Example
string message = "123";
Console.WriteLine(message.PadLeft(5, '0')); // Outputs "00123"
Console.WriteLine(message.PadRight(6, '-')); // Outputs "123---"
Output:
00123
123---
Code Explanation: The PadLeft()
method pads the string with zeros on the left to ensure it has a length of 5, while PadRight()
pads the string with dashes on the right to ensure a length of 6.