Updated April 14, 2023
Introduction to C# String Format()
The value of a variable or an expression or an object can be inserted into another string by using a method called string Format method in C#. By using the string format method, the format items of the string specified will be replaced by the string representation of the objects specified. There are several types of String Format methods like Date Time Format method, Number Format method, Custom Format method, etc. By using these different types of format methods, the format items can be replaced by the corresponding representation of the objects in C# programming language.
Syntax:
The syntax of the C# String Format method is as follows:
public string Format(string, object)
public string Format(string, object, object)
public string Format(IFormatProvider, string, object)
Where the first syntax of the format method is used to replace the format items of the specified string with the string representation of the specified object. The second syntax of the format method is used to replace the format items of the specified string with the string representation of the two specified objects. The third syntax of the format method is used to replace the format items of the specified string with the string representation of the corresponding objects.
Functions of C# String Format Method
- Whenever there is a need to format the string by replacing it with the string representation of different objects, we make use of the string format method.
- By using the string format method, the format items of the specified string can be replaced with the string representation of the specified object.
- By using the string format method, the format items of the specified string can be replaced with the string representation of the two specified objects.
- By using the string format method, the format items of the specified string can be replaced with the string representation of the corresponding object.
- There are several formats in which the format items of the string can be specified. There are number formats, date and time formats, and custom formats as well.
Examples of C# String Format()
Following are the examples are given below:
Example #1
C# program to demonstrate the string format method to replace the format items of the specified string with more than two objects:
Code:
using System;
//a namespace called program is defined
namespace program
{
//a class called check is defined
class check
{
//main method is called
static void Main(string[] args)
{
//a string variable is used to store the format items that needs to be replaced with the string representation of objects
string str = "{0} {1:0.0%}";
//string format method is used to replace the format items of the specified string with the string representation of objects
string res = string.Format(str, "India has a total power consumption of", 0.73);
Console.WriteLine("The statement after using the string format method is:");
Console.WriteLine("\n {0}",res);
Console.ReadLine();
}
}
}
Output:
In the above program, a namespace called program is created. Then a class called check is created within which the main method is called. Inside the main method, a string variable is defined to store the format items that needs to be replaced by the string representation of the objects. One of the format strings is specified with % sign, meaning it multiplies the given value by 100 and gives the product as a result. Hence as it can be seen in the output, we have obtained 73.0% when the format item is 0.0%. Then the string format method is used to replace the format items of the string with the string representation of the specified objects.
Example #2
C# program to demonstrate the string format method to replace the format items of the specified integer value with the hexadecimal representation and to display the date and time format by using DateTime.Now property:
Code:
using System;
//a namespace called program is defined
namespace program
{
//a class called check is defined
class check
{
//main method is called
static void Main(string[] args)
{
//an integer variable is used to store the value
int value = 200;
//hexadecimal format method is used to replace the format items of the specified integer value with the hexadecimal representation of objects
Console.WriteLine("The statement after using the hex format method is {0:x}", value);
//DateTime.Now is used to obtain the current date and time by creating an instance of it
DateTimedt = DateTime.Now;
Console.WriteLine("The current date and time is: {0}", dt);
//By using date format which can display only the date, the current date is displayed
Console.WriteLine("The current date is: {0:D}", dt);
//By using time format which can display only the time, the current time is displayed
Console.WriteLine("The current time is: {0:T}", dt);
//a string variable is used to store the values for padding, here negative values indicate left alignment and positive values indicate right alignment
string hey = "{0,-40} {0,40}";
string res = string.Format(hey,"This is describing padding");
Console.WriteLine(res);
Console.ReadLine();
}
}
}
Output:
In the above program, a namespace called program is created. Then a class called check is created within which the main method is called. Inside the main method, an integer variable is defined to store the integer which needs to be converted to hexadecimal format. Then the hexadecimal format method is used to replace the format item by the hexadecimal representation of the object. Then DateTime.Now is used to obtain the current date and time by creating an instance of it. Then by using the date format which can display only the date, the current date is displayed. Then by using time format which can display only the time, the current time is displayed. Then a string variable is used to store the values for padding where negative values indicate left alignment and positive values indicate right alignment. The output is as shown in the snapshot above.
Recommended Articles
This is a guide to C# String Format(). Here we also discuss the introduction and working of the c# string format method along with different examples and its code implementation. You may also have a look at the following articles to learn more –