ASP.Net C# String Format Function

Updated on 18 May 2012,
Published on 04 Jul 2008

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.

String Functions Examples:

You can see the live samples and examples of String functions from the following links:

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# String.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.

Output:

Learn with the help of live sample of C# Format String Function from the following links:

String Format Function 01

String Format Function 02

Continue to next tutorial: ASP.Net C# String Compare Function to learn how to compare 2 string values.

9 Responses to "ASP.Net C# String Format Function"
mani
Hi,

4,500 how to convert 4500
(400) how to convert -400
Ezineasp.net
Hi Mani

You can try the following code to parse the int:


// string type number value in Parantheses
string number = "(10)";

// int Parse function with parameter System.Globalization.NumberStyles to allow leading sign and Parantheses

int parseNumber = int.Parse(number, System.Globalization.NumberStyles.AllowLeadingSign | System.Globalization.NumberStyles.AllowParentheses);

Response.Write(parseNumber);


// string type number value with comma separation for hundreds and thousands
number = "45,000";

// int Parse function with parameter System.Globalization.NumberStyles to allow thousands separation

parseNumber = int.Parse(number, System.Globalization.NumberStyles.AllowThousands);

Response.Write(parseNumber);


[b]Output:[/b]

-10

45000
waleed
Hi every one i'm new in the Asp.NET and i need to parse int to string but it's make error i know there is some thing wrong but i cant't solve it

such as :




int.parse(g)= TextBox.Text

can any one help please?
Ezineasp.net
Hi Waleed Try to use TextBox.Text = g.ToString() Hope this will work.
waleed
Hi all thanx for replying my question but still have probllem

that i have variable g and it's data type int
and i need to convert it to string

some one told me to use this code
but it dosen't work with me at all

TextBox.Text = g.ToString()
so i hope that any one can give me a small help i'll be very greatfull
thanx all
Ezineasp.net
Hi Waleed Please explain your problem, what is the exact issue? g.ToString() is the right way to convert the int type variable to string. What are you trying to parse?
Please try with this,

TextBox.Text = X.ToString()

Thanks
-Nimesh. []
Daya
Thank's Dear. I am write code formatexpression(string.expretion,"dd-MMM-yyyy") then it is show error. thanks for that.
kaushi
hi ,
how to convert a valou such as eg. 1,000.0000 into simple 1000.
plz help!!
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email