Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
String Formatting in C# to add padding
String formatting with padding in C# allows you to align text in columns by adding spaces to the left or right of strings. This is particularly useful for creating formatted tables, reports, or aligned output displays.
Padding is controlled by format specifiers that define the width and alignment of each placeholder in the format string.
Syntax
The basic syntax for padding in format strings is −
string.Format("{index,width}", value)
Where −
- index − The parameter index (0, 1, 2, etc.)
- width − The total width of the field
- Positive width − Right-aligned (left padding)
- Negative width − Left-aligned (right padding)
Using Left-Aligned Padding
Example
using System;
public class Program {
public static void Main() {
// Left-aligned padding using negative width
const string format = "{0,-10} {1,-15}";
string header = string.Format(format, "Name", "Department");
string row1 = string.Format(format, "Alice", "Engineering");
string row2 = string.Format(format, "Bob", "Marketing");
Console.WriteLine(header);
Console.WriteLine(row1);
Console.WriteLine(row2);
}
}
The output of the above code is −
Name Department Alice Engineering Bob Marketing
Using Right-Aligned Padding
Example
using System;
public class Program {
public static void Main() {
// Right-aligned padding using positive width
const string format = "{0,8} {1,10:C}";
string header = string.Format("{0,8} {1,10}", "Item", "Price");
string item1 = string.Format(format, "Apple", 1.50);
string item2 = string.Format(format, "Banana", 0.75);
Console.WriteLine(header);
Console.WriteLine(item1);
Console.WriteLine(item2);
}
}
The output of the above code is −
Item Price
Apple $1.50
Banana $0.75
Mixed Alignment Table
Example
using System;
public class Program {
public static void Main() {
// Mixed alignment: left-aligned names, right-aligned numbers
const string format = "{0,-8} {1,5} {2,8}";
string header = string.Format(format, "Rank", "Name", "Score");
string row1 = string.Format(format, "1st", "Tom", "95");
string row2 = string.Format(format, "2nd", "Sarah", "87");
string row3 = string.Format(format, "3rd", "Mike", "82");
Console.WriteLine(header);
Console.WriteLine("------------------------");
Console.WriteLine(row1);
Console.WriteLine(row2);
Console.WriteLine(row3);
}
}
The output of the above code is −
Rank Name Score ------------------------ 1st Tom 95 2nd Sarah 87 3rd Mike 82
Comparison of Padding Methods
| Method | Syntax | Alignment | Use Case |
|---|---|---|---|
| Negative Width | {0,-10} |
Left-aligned | Text labels, names |
| Positive Width | {0,10} |
Right-aligned | Numbers, currency |
| PadLeft() | text.PadLeft(10) |
Right-aligned | Simple right padding |
| PadRight() | text.PadRight(10) |
Left-aligned | Simple left padding |
Conclusion
String formatting with padding in C# uses width specifiers in format strings to align text. Negative widths create left-aligned text with right padding, while positive widths create right-aligned text with left padding. This technique is essential for creating well-formatted tables and aligned console output.
