Showing posts with label PadLeft. Show all posts
Showing posts with label PadLeft. Show all posts

October 2, 2026

PadLeft and PadRight

Just a simple example to show what PadLet and PadRight do:


void Main()
{  
	int[] numbers = { 1, 12, 123, 1234, 12345, 123456, 1234567, 12345678, 123456789, 1234567890 };

    DisplayPad(numbers, '-', 9);
}

public static void DisplayPad(IEnumerable numbers, char padChar = ' ', int padSize = 8)
{
    foreach (var number in numbers)
    {
        var none = number.ToString();
        var left = none.PadLeft(padSize, padChar);
        var right = none.PadRight(padSize, padChar);
        Console.WriteLine($"|{left}|{none}|{right}|");
    }
}
Here is what the output looks like:
|--------1|1|1--------|
|-------12|12|12-------|
|------123|123|123------|
|-----1234|1234|1234-----|
|----12345|12345|12345----|
|---123456|123456|123456---|
|--1234567|1234567|1234567--|
|-12345678|12345678|12345678-|
|123456789|123456789|123456789|
|1234567890|1234567890|1234567890|