Format string in C# Language with examples

Format string using Interpolation$ in C Language

Today in this article we shall see how to use the String Interpolation feature for performing various string operations like formatting, concatenation, string building, etc.

So we shall see overall below details in today article,

String interpolation creates formatted strings that provide a more readable and convenient syntax compared to conventional methods of formatting.

This feature is available starting with C# 6 and above.

To identify a string as an interpolated string, prepend it with the $ symbol.

Example:

Using String Format method :

Console.WriteLine(String.Format("Hello, {0}! .NET Core {1} is released on {2} .", name, netCoreVersion, date));

Composite Formatting :

Console.WriteLine("Hello, {0}! .NET Core {1} is released on {2} .", name, netCoreVersion, date.ToString());

Using String Interpolation:

Console.WriteLine($"Hello, { name } ! .NET Core { netCoreVersion } is released on { date.ToString()} .");
Format string in C Language with examples

Naming Pattern:

{<interpolationExpression>[,<alignment>][:<formatString>]}

Details,

  • InterpolationExpression – expression that produces a result to be formatted
  • alignment – If positive, the string representation is right-aligned; if negative, it’s left-aligned
  • formatString – A format string that is supported by the type of the expression result

String Format – Using Ternary Conditional operator and Special character

Example 1:

Using Conditional operator:

The below expression validates null reference validation in $ interpolation string also demonstrates the ternary operator inside an interpolated string.

Console.WriteLine($"Saving AccountNumber is: {(savingAccount == null ? "Not Available" : savingAccount.AccountNumber)}");

Result:

Saving AccountNumber is: 1234

String Format – Including braces “” in the $ interpolation string

Example

 Console.WriteLine($"Saving AccountNumber is: \" {savingAccount.AccountNumber} \" ");

Result :

Saving AccountNumber is: " 1234 "

String format – Iterating over the array, List or Dictionary in Interpolation

Example:

var employeeList = new int[] { 1456, 2234, 7232, 2319 };
Console.WriteLine($"Employee Numbers are {string.Join(", ", employeeList)}");

Result:

Employee Number are {1456, 2234, 7232, 2319}

The above pattern works for generics like List or Dictionary easily.

String Format – Specifying file path using Interpolation

Example:

          string UserName = "TheCodeBuzz";
          var Filepath = $"C:\\Users\\{UserName}\\Documents";
          Console.WriteLine(Filepath);

Result:

C:\Users\TheCodeBuzz\Documents

String Format – Adding String New Line break

We can use Environment.NewLine for string break or newline character.

Example

      var employeeList = new int[] { 1456, 2234, 7232, 2319 };

     Console.WriteLine($"Employee Numbers are{Environment.NewLine}{string.Join(Environment.NewLine, employeeList)}");

Using String Interpolation$ in C Language

That’s all! Happy coding!

Reference:

Summary

Today in this article we briefed through C# language $ Interpolation and understood how to use it. The string Interpolation feature has simplified performing various string operations like formatting, string concatenation, an enumeration of list or array, or new line break of a given string with a readable and easy to write very convenient syntax.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *