What does {0} do?

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?
 
P

Patrice

This is a placeholder. {0} will be replaced by val, {1} by DateTime.Now
etc...

Patrice
 
F

Florian Esser

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
 
L

Lau Lei Cheong

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.
 
V

VB Programmer

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.
 
V

VB Programmer

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

Versus just doing this

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

?
 
H

Hans Kesting

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
 
P

Patrice

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top