ASP.Net C# String Format function is used to build a dynamic string with formatted text that takes number of dynamic parameters to be filled at the runtime. In ASP.Net 2.0 you can use the C# string format function to display a dynamic string whose parameter depend on the returned values of other functions. You can pass upto 3 object type args to the string.Format function that builds the string. Curly brackets { } are used along with index of argument whose value is automatically filled at its defined place e.g. {0} {1} {2}. Value of first object argument is passed at the place of {0}, second argument value is passed at {2} and so on. If there are more than 3 value parameters then you can create an array of argument values that can be passed as a collection of arguments to the string format function.
Following are the overloads for the C# string.Format Function:
string. Format("Hello {0} !!!", "World");
Above sample syntax example code shows the use of string.Format function that will generate the Output as Hello World!!!
string arg0 = "arg0"; string arg1 = "arg1"; string arg2 = "arg2"; Response.Write(string.Format("String Format Function Passed with 1 Arg: {0} <br />", arg0)); Response.Write(string.Format("String Format Function Passed with 2 Args: {0} and {1} <br />", arg0, arg1)); Response.Write(string.Format("String Format Function Passed with 3 Args: {0}, {1}, {2} <br />", arg0, arg1, arg2));
string arg3 = "arg3"; string[] args = new string[] { arg0, arg1, arg2, arg3 }; Response.Write(string.Format("String Format Function Passed using Array of 4 Args: {0}, {1}, {2}, {3} <br />", args));
Response.Write(string.Format(System.Globalization.CultureInfo.CurrentCulture,"String Format Function with CultureInfo IFormatProvider and Array of 4 Args: {0}, {1}, {2}, {3} <br />" , args)); Response.Write(string.Format("String Format Function to Convert Decimal to Hexadecimal: {0:X} <br />", 255)); Response.Write(string.Format("String Format Function to Convert Decimal to Scientific Number: {0:E} <br />", 255)); Response.Write(string.Format("String Format Function to Convert Decimal to Number Format: {0:N} <br />", 255)); Response.Write(string.Format("String Format Function to Convert Number to Currency: {0:C} <br />", 255)); Response.Write(string.Format("String Format Function to Convert to Number Format: {0:000,000.00} <br />", 123456789));
Output
String Format Function Passed with 1 Arg: arg0 String Format Function Passed with 2 Args: arg0 and arg1 String Format Function Passed with 3 Args: arg0, arg1, arg2 String Format Function Passed using Array of 4 Args: arg0, arg1, arg2, arg3 String Format Function with CultureInfo IFormatProvider and Array of 4 Args: arg0, arg1, arg2, arg3 String Format Function to Convert Decimal to Hexadecimal: FF String Format Function to Convert Decimal to Scientific Number: 2.550000E+002 String Format Function to Convert Decimal to Number Format: 255.00 String Format Function to Convert Number to Currency: $255.00 String Format Function to Convert to Number Format: 123,456,789.00
In the examples you can see that we have used some format types such as {0:X}, {0:E}, {0:N}, {0:C}. These formats work as follows:
{0:X} converts decimal number to hexadecimal.
{0:E} converts decimal number to scientific exponential form.
{0:N} converts decimal number to number format.
{0:C} converts number to currency format.
Be the first to rate this post
Tags: asp.net 2.0, c#, c# string format, c# string function, asp.net string format function, asp.net string function, c# split, c# lastindexof, c# indexof, c# replace, c# remove, c# tutorials, asp.net examples, c# convert decimal to hex, c# convert decimal to number, c# convert number to currency
8/23/2008 8:41:50 PM