String.Format() Method in C# with Examples | Set – 3
In C#, Format() is a string method. This method is used to replace one or more format items in the specified string with the string representation of a specified object.In other words, this method is used to insert the value of the variable or an object or expression into another string.
This method can be overloaded by passing different type of arguments to it. There are total 8 methods in the overload list of the Format() method in which 6 are discussed in Set-1 and Set-2, and remaining are discussed in this article.
- String.Format(String first, Object second) Method
- String.Format(String, params Object[]) Method
- String.Format(IFormatProvider, String, Object) Method
- String.Format(IFormatProvider, String, Object[]) Method
- String.Format(String, Object, Object) Method
- String.Format(String, Object, Object, Object) Method
- String.Format(IFormatProvider, String, Object, Object) Method
- String.Format(IFormatProvider, String, Object, Object, Object) Method
String.Format(IFormatProvider, String, Object, Object) Method
This method is used to replaces the format items in a string with the string representation of two specified objects. A parameter supplies culture-specific formatting information.
Syntax :
public static string Format (IFormatProvider provider, string format, object arg0, object arg1);
Parameter: This method has the following parameters:
provider: This parameter is the object that supplies culture-specific formatting information.
format: This parameter is the required composite format string.
arg0: This parameter is the first object to format.
arg1: This parameter is the second object to format.
Return Value: This method returns the string. It is a copy of format in which the format items are replaced by the string representation of arg0 and arg1.
Example :
C#
// C# program to illustrate the // String.Format(IFormatProvider, // String, Object, Object) Method using System; public class GFG { // Main method public static void Main( string [] args) { string formatString = "Value: {0,0}\n" + "NOT of Value: {1,0}" ; int value1 = 169; System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo( "es-ES" ); string result = String.Format (culture, formatString, value1, ~value1); Console.WriteLine(result); } } |
Output:
Value: 169 NOT of Value: -170
String.Format(IFormatProvider, String, Object, Object, Object) Method
This method is used to replaces the format items in a string with the string representation of three specified objects. An parameter supplies culture-specific formatting information.
Syntax :
public static string Format (IFormatProvider provider, string format, object arg0, object arg1, object arg2);
Parameter: This method has the following parameters:
provider: This parameter is the object that supplies culture-specific formatting information.
format: This parameter is the required composite format string.
arg0: This parameter is the first object to format.
arg1: This parameter is the second object to format.
arg2: This parameter is the third object to format.
Return Value: This method returns the string. It is a copy of format in which the format items are replaced by the string representation of arg0, arg1 and arg2.
Example :
C#
// C# program to illustrate the // String.Format(IFormatProvider, // String, Object, Object, Object) Method using System; public class GFG { // Main method public static void Main( string [] args) { string formatString = "Value 1: {0,0}\n" + "Value 2: {1,0}\n" + "Sum of Values : {2,0}" ; int value1 = 169; int value2 = 961; System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo( "es-ES" ); string result = String.Format (culture, formatString, value1, value2, value1 + value2); Console.WriteLine(result); } } |
Output:
Value 1: 169 Value 2: 961 Sum of Values : 1130
Please Login to comment...