ASP.Net C# String Format Function

by top54u.com 29 Jun, 2008
Spotlight.....

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:

 

  1. public static string Format(string format, object arg0);

  2. public static string Format(string format, params object[] args);

  3. public static string Format(IFormatProvider provider, string format, params object[] args);

  4. public static string Format(string format, object arg0, object arg1);

  5. public static string Format(string format, object arg0, object arg1, object arg2);

 

 

Syntax for ASP.Net 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!!!

 

Examples of ASP.Net C# Srting.Format Function

 

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.

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

8/23/2008 8:41:50 PM

OUR SPONSORS[+ advertise here]
related videos.....
recent posts.....
top54u ezines.....