0
0
CsharpHow-ToBeginner · 3 min read

How to Pad String in C#: Simple Methods and Examples

In C#, you can pad a string using the PadLeft and PadRight methods to add characters to the left or right until the string reaches a desired length. These methods take the total length and an optional padding character as arguments.
📐

Syntax

The PadLeft and PadRight methods add padding characters to a string to reach a specified total length.

  • string.PadLeft(int totalWidth): Pads the string on the left with spaces until it reaches totalWidth.
  • string.PadLeft(int totalWidth, char paddingChar): Pads the string on the left with the specified paddingChar.
  • string.PadRight(int totalWidth): Pads the string on the right with spaces until it reaches totalWidth.
  • string.PadRight(int totalWidth, char paddingChar): Pads the string on the right with the specified paddingChar.
csharp
string paddedLeft = "text".PadLeft(8); // pads with spaces on the left
string paddedLeftChar = "text".PadLeft(8, '*'); // pads with '*' on the left
string paddedRight = "text".PadRight(8); // pads with spaces on the right
string paddedRightChar = "text".PadRight(8, '-'); // pads with '-' on the right
💻

Example

This example shows how to pad a string on the left and right using spaces and custom characters.

csharp
using System;

class Program
{
    static void Main()
    {
        string original = "42";

        string leftPadded = original.PadLeft(5); // pads with spaces
        string leftPaddedChar = original.PadLeft(5, '0'); // pads with '0'

        string rightPadded = original.PadRight(5); // pads with spaces
        string rightPaddedChar = original.PadRight(5, '*'); // pads with '*'

        Console.WriteLine($"'{leftPadded}'");
        Console.WriteLine($"'{leftPaddedChar}'");
        Console.WriteLine($"'{rightPadded}'");
        Console.WriteLine($"'{rightPaddedChar}'");
    }
}
Output
' 42' '00042' '42 ' '42***'
⚠️

Common Pitfalls

Common mistakes include:

  • Using a totalWidth smaller than the string length, which returns the original string unchanged.
  • Forgetting that padding adds characters until the total length is reached, not the number of characters added.
  • Assuming PadLeft and PadRight modify the original string (strings are immutable in C#).
csharp
string s = "hello";

// Wrong: totalWidth less than string length, no padding added
string wrongPad = s.PadLeft(3); // returns "hello"

// Correct: totalWidth greater than string length
string correctPad = s.PadLeft(8, '-'); // returns "---hello"
📊

Quick Reference

MethodDescriptionExample
PadLeft(int totalWidth)Pads string on the left with spaces"text".PadLeft(6) → " text"
PadLeft(int totalWidth, char paddingChar)Pads string on the left with specified char"text".PadLeft(6, '*') → "**text"
PadRight(int totalWidth)Pads string on the right with spaces"text".PadRight(6) → "text "
PadRight(int totalWidth, char paddingChar)Pads string on the right with specified char"text".PadRight(6, '-') → "text--"

Key Takeaways

Use PadLeft and PadRight to add padding characters to the left or right of a string.
Specify total length and optional padding character; default is space.
If total length is less than string length, the original string is returned unchanged.
Strings are immutable; padding methods return new strings without changing the original.
PadLeft and PadRight help format strings for alignment in output.