| by Arround The Web | No comments

How to Use String.Format Method C#

String formatting is an important aspect of any programming language, including C# as it allows programmers to format text strings by replacing placeholders with values or expressions. The String.Format() method is one of the most utilized ways for formatting strings in C#, this article will discuss what this method is and how to use it.

What is String.Format() Method

By substituting the format items with the string representation of the values of the associated object, this method aids in formatting a string. This method returns a formatted string by replacing the placeholders in the string with values of the objects provided.

How to Use String.Format() Method

The format string and a variety of arguments are inputs into the String.Format() method. Braced placeholders (” “) denote locations in the format string where the values of the objects should be placed. The placeholders are replaced by the arguments supplied to the method in the order they appear in the format string, here is an example of how to use this method in C#:

using System;

class myClass {
    static void Main(string[] args) {
       
        string ID = "SAM";
        int age = 25;
        string message = String.Format("My ID is {0} and I am {1}", ID, age);
        Console.WriteLine(message);
    }
}

In the above code, we first declare a string variable called “ID” and an integer variable called “age”. We then use the string formatting function to format a string containing placeholders for the values of these variables.

The formatted string containing the placeholder values “name” and “age” will be found in the message variable.

You can format strings, integers, floating-point numbers, and dates using a variety of format specifiers, here is an example that uses the following format specifiers:

  • {1:D2} is used to format the integer age as a decimal number with at least 2 digits, padding with leading zeros if necessary.
  • {2:F2} is used to format the double height as a fixed-point number with exactly 2 decimal places.
  • {3:MM/dd/yyyy} is used to format the birthDate object as a string in the format month/day/year, with leading zeros for the month and day.
using System;

class myClass
{
    static void Main(string[] args)
    {
        string name = "Sam";
        int age = 20;
        double height = 6.1;
        DateTime birthDate = new DateTime(1998, 4, 15);
        string message = String.Format("My name is {0} and I am {1:D2} years old. My height is {2:F2} feet. My birthdate is {3:MM/dd/yyyy}.", name, age, height, birthDate);
        Console.WriteLine(message);
    }
}

This code defines a C# program that uses the String.Format method to format a string with various placeholders that get replaced with values of different data types such as string, integer, double, and DateTime.

The placeholders are specified using curly braces {} with an index indicating the position of the corresponding value in the list of arguments passed to the String.Format method. The D2 and F2 format specifiers are used to format the integer and double values respectively with leading zeros and two decimal places. The MM/dd/yyyy format specifier is used to format the date value in the month/day/year format.

Conclusion

The String.Format() method is a handy tool for formatting strings in C#. It allows programmers to easily replace placeholders in a format string with the values of corresponding variables or expressions. Using this method can help improve the readability and clarity of code, especially when dealing with complex output messages or user interfaces.

Share Button

Source: linuxhint.com

Leave a Reply