String Formatting in C# to add padding on the right

String formatting in C# provides several ways to add padding to strings. Right padding aligns text to the left by adding spaces to the right side of the string until it reaches the specified width.

Syntax

Following is the syntax for right padding using composite formatting −

string.Format("{0,-width}", value)

Following is the syntax for right padding using string interpolation −

$"{value,-width}"

Following is the syntax for right padding using the PadRight() method −

string.PadRight(totalWidth)
string.PadRight(totalWidth, paddingChar)

Right Padding Visualization Hello spaces Original Text Right Padding Total Width: 10 characters

Using String.Format() with Composite Formatting

The composite formatting approach uses negative alignment values for right padding −

Example

using System;

public class Program {
    public static void Main() {
        // Right padding using negative alignment
        const string format = "{0,-15}";
        string str1 = string.Format(format, "Product");
        string str2 = string.Format(format, "Price");
        string str3 = string.Format(format, "Laptop");
        string str4 = string.Format(format, "$999");

        Console.WriteLine(str1 + "|");
        Console.WriteLine(str2 + "|");
        Console.WriteLine(str3 + "|");
        Console.WriteLine(str4 + "|");
    }
}

The output of the above code is −

Product        |
Price          |
Laptop         |
$999           |

Using String Interpolation

String interpolation provides a more readable syntax for right padding −

Example

using System;

public class Program {
    public static void Main() {
        string name = "Alice";
        string department = "Engineering";
        int score = 95;

        // Right padding with string interpolation
        Console.WriteLine($"{name,-12} | {department,-15} | {score,-5}");
        Console.WriteLine($"{"Bob",-12} | {"Marketing",-15} | {87,-5}");
        Console.WriteLine($"{"Charlie",-12} | {"Sales",-15} | {92,-5}");
    }
}

The output of the above code is −

Alice        | Engineering     | 95   
Bob          | Marketing       | 87   
Charlie      | Sales           | 92   

Using PadRight() Method

The PadRight() method offers direct control over padding characters and width −

Example

using System;

public class Program {
    public static void Main() {
        string text = "Hello";
        
        // Basic right padding with spaces
        Console.WriteLine("'" + text.PadRight(10) + "'");
        
        // Right padding with custom character
        Console.WriteLine("'" + text.PadRight(10, '-') + "'");
        Console.WriteLine("'" + text.PadRight(12, '*') + "'");
        
        // Padding shorter than original text (no effect)
        Console.WriteLine("'" + text.PadRight(3) + "'");
    }
}

The output of the above code is −

'Hello     '
'Hello-----'
'Hello*******'
'Hello'

Comparison of Right Padding Methods

Method Syntax Best For
String.Format() "{0,-width}" Complex formatting with multiple values
String Interpolation $"{value,-width}" Modern, readable code with embedded expressions
PadRight() string.PadRight(width) Simple padding, custom characters

Conclusion

Right padding in C# can be achieved using composite formatting with negative alignment, string interpolation, or the PadRight() method. Choose PadRight() for simple cases, string interpolation for modern readable code, and composite formatting for complex scenarios with multiple formatted values.

Updated on: 2026-03-17T07:04:35+05:30

322 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements