What does {0} do?

  • Thread starter Thread starter VB Programmer
  • Start date Start date
V

VB Programmer

In VB.NET code examples I've repeatedly seen numbers between 2 curly braces,
such as {0}, {1}, etc...

Example:
Dim str As String = String.Format("Days Left : {0}. Current DataTime: {1:u}.
\n String: {2}, Float: {3}", val, DateTime.Now, name, num)

What does this mean?
 
In VB.NET code examples I've repeatedly seen numbers between 2 curly
braces,
such as {0}, {1}, etc...

Example:
Dim str As String = String.Format("Days Left : {0}. Current DataTime: {1:u}.
\n String: {2}, Float: {3}", val, DateTime.Now, name, num)

What does this mean?

You might want to take a look at
http://msdn.microsoft.com/library/d...f/html/frlrfsystemstringclassformattopic2.asp.

The MSDN Library is, by the way, usually the first place to look when you
have a syntax question ;)

HTH,

--Florian

===================================
If you wish to contact me directly, send your mail to
florian DOT esser AT gmx DOT de
 
They mean the order of variables to be inserted in the string.

For example if you try String.Format(" A is {0} and B is {1}, but when
exchanged A is {1} and B is {0}, "A", "B") you'll see what I mean.
 
Thanks everyone!

Lau Lei Cheong said:
They mean the order of variables to be inserted in the string.

For example if you try String.Format(" A is {0} and B is {1}, but when
exchanged A is {1} and B is {0}, "A", "B") you'll see what I mean.
 
Why do this:
Response.Write(String.Format("First:{0}, Second:{1}", Var1, Var2))

Versus just doing this

Response.Write("First:" & Var1 & ", Second:" & Var2)

?
 
VB Programmer said:
Why do this:
Response.Write(String.Format("First:{0}, Second:{1}", Var1, Var2))

Versus just doing this

Response.Write("First:" & Var1 & ", Second:" & Var2)

?

1) easier to read (for humans)
2) probably (can anyone confirm?) better performance than pasting together a lot of strings

Hans Kesting
 
Flexibility. You have just to use "Premier : {0}, second : {1}." for the
French version or you can use "{1} comes after {0}." instead.

Especially if the format string itself is not hardcoded in the application,
it doesn't even require a code change.

Patrice
 
Back
Top