Home > Backend Development > C#.Net Tutorial > C# String Format()

C# String Format()

王林
Release: 2024-09-03 15:15:46
Original
1039 people have browsed it

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)
Copy after login

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();
}
}
}
Copy after login

Output:

C# String Format()

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();
}
}
}
Copy after login

Output:

C# String Format()

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.

The above is the detailed content of C# String Format(). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template