Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Building Formatted Strings | Text Formatting & Manual Parsing
C# Strings & Text Processing

bookBuilding Formatted Strings

When creating reports, displaying tables, or building user interfaces, you often need to present information in a clear and visually organized way. Rather than simply joining strings together, you can use advanced formatting techniques to control how text, numbers, and other values appear. This is especially important when you want data to line up in columns or follow a specific format, making your output easier to read and more professional.

Program.cs

Program.cs

copy
1234567891011121314151617181920212223
// File: Program.cs using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { string header = string.Format("{0,-15} {1,10} {2,15}", "Product", "Qty", "Price"); string row1 = string.Format("{0,-15} {1,10} {2,15:C}", "Book", 12, 19.95); string row2 = string.Format("{0,-15} {1,10} {2,15:C}", "Pen", 100, 1.25); string row3 = string.Format("{0,-15} {1,10} {2,15:C}", "Notebook", 50, 3.99); Console.WriteLine(header); Console.WriteLine(new string('-', 42)); Console.WriteLine(row1); Console.WriteLine(row2); Console.WriteLine(row3); } } }

The string.Format method uses composite formatting, which means you place numbered placeholders like {0} in your format string. Each placeholder corresponds to a value provided after the format string. You can also use format specifiers and alignment options inside these placeholders. For example, {1,10} means the value will be right-aligned in a field of 10 characters, while {0,-15} means left-aligned in 15 characters. This lets you control column widths and alignment, which is especially useful for tabular data. Placeholders can also include format specifiers such as :C for currency, :N2 for numbers with two decimal places, or :d for dates.

Program.cs

Program.cs

copy
1234567891011121314151617181920
// File: Program.cs using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { double price = 1234.567; DateTime today = DateTime.Now; int quantity = 42; string formatted = string.Format("Price: {0:C2} | Date: {1:yyyy-MM-dd} | Qty: {2:N0}", price, today, quantity); Console.WriteLine(formatted); } } }

Custom formatting strings allow you to display values exactly how you want. For instance, you can specify the number of decimal places for a floating-point number, show a date in a particular style, or present a number as currency. By combining alignment and format specifiers, you can create professional-looking tables or summaries. This flexibility is especially useful when generating reports or exporting data for users.

Note
Definition

Composite formatting is a technique where you use a format string with indexed placeholders (like {0}) and provide corresponding values. It is more powerful and readable than simple string concatenation because it lets you align, format, and insert multiple values efficiently in a single operation.

Program.cs

Program.cs

copy
123456789101112131415161718
// File: Program.cs using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int age = 30; double salary = 45678.9; string name = "Dana"; // Using string interpolation with format specifiers: Console.WriteLine($"{name,-10} | Age: {age,3} | Salary: {salary:C}"); } } }

String interpolation is another way to build formatted strings. By prefixing a string with $, you can embed expressions directly inside curly braces, and you can still use alignment and format specifiers. This often makes code easier to read and write, especially when combining multiple variables and formatting options.

1. What does {0,10} mean in a format string?

2. Which method is used for composite formatting?

3. How can you include a variable's value inside a string using interpolation?

question mark

What does {0,10} mean in a format string?

Select the correct answer

question mark

Which method is used for composite formatting?

Select the correct answer

question mark

How can you include a variable's value inside a string using interpolation?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 1

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

bookBuilding Formatted Strings

Sveip for å vise menyen

When creating reports, displaying tables, or building user interfaces, you often need to present information in a clear and visually organized way. Rather than simply joining strings together, you can use advanced formatting techniques to control how text, numbers, and other values appear. This is especially important when you want data to line up in columns or follow a specific format, making your output easier to read and more professional.

Program.cs

Program.cs

copy
1234567891011121314151617181920212223
// File: Program.cs using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { string header = string.Format("{0,-15} {1,10} {2,15}", "Product", "Qty", "Price"); string row1 = string.Format("{0,-15} {1,10} {2,15:C}", "Book", 12, 19.95); string row2 = string.Format("{0,-15} {1,10} {2,15:C}", "Pen", 100, 1.25); string row3 = string.Format("{0,-15} {1,10} {2,15:C}", "Notebook", 50, 3.99); Console.WriteLine(header); Console.WriteLine(new string('-', 42)); Console.WriteLine(row1); Console.WriteLine(row2); Console.WriteLine(row3); } } }

The string.Format method uses composite formatting, which means you place numbered placeholders like {0} in your format string. Each placeholder corresponds to a value provided after the format string. You can also use format specifiers and alignment options inside these placeholders. For example, {1,10} means the value will be right-aligned in a field of 10 characters, while {0,-15} means left-aligned in 15 characters. This lets you control column widths and alignment, which is especially useful for tabular data. Placeholders can also include format specifiers such as :C for currency, :N2 for numbers with two decimal places, or :d for dates.

Program.cs

Program.cs

copy
1234567891011121314151617181920
// File: Program.cs using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { double price = 1234.567; DateTime today = DateTime.Now; int quantity = 42; string formatted = string.Format("Price: {0:C2} | Date: {1:yyyy-MM-dd} | Qty: {2:N0}", price, today, quantity); Console.WriteLine(formatted); } } }

Custom formatting strings allow you to display values exactly how you want. For instance, you can specify the number of decimal places for a floating-point number, show a date in a particular style, or present a number as currency. By combining alignment and format specifiers, you can create professional-looking tables or summaries. This flexibility is especially useful when generating reports or exporting data for users.

Note
Definition

Composite formatting is a technique where you use a format string with indexed placeholders (like {0}) and provide corresponding values. It is more powerful and readable than simple string concatenation because it lets you align, format, and insert multiple values efficiently in a single operation.

Program.cs

Program.cs

copy
123456789101112131415161718
// File: Program.cs using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int age = 30; double salary = 45678.9; string name = "Dana"; // Using string interpolation with format specifiers: Console.WriteLine($"{name,-10} | Age: {age,3} | Salary: {salary:C}"); } } }

String interpolation is another way to build formatted strings. By prefixing a string with $, you can embed expressions directly inside curly braces, and you can still use alignment and format specifiers. This often makes code easier to read and write, especially when combining multiple variables and formatting options.

1. What does {0,10} mean in a format string?

2. Which method is used for composite formatting?

3. How can you include a variable's value inside a string using interpolation?

question mark

What does {0,10} mean in a format string?

Select the correct answer

question mark

Which method is used for composite formatting?

Select the correct answer

question mark

How can you include a variable's value inside a string using interpolation?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 1
some-alt