Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do numbers in braces e.g. "{0}" mean?

I've been looking around but having great difficulty finding the answer to this question as the thing I'm looking for is so unspecific.

I've seen a lot of code which uses {0} in it, and I still can't work out what it's doing. Here's an example:

Dim literal As String = "CatDogFence"
Dim substring As String = literal.Substring(6)
Console.WriteLine("Substring: {0}", substring)
like image 762
Lou Avatar asked Jun 01 '13 12:06

Lou


People also ask

What are these {} called?

These are braces { }. They are sometimes called "curly brackets." They are rarely used in writing, but they can be used to show list items or equal choices.

What are the {} brackets called?

The "{}" are referred to as curly brackets or braces while "<>" are often called angle brackets or braces. The term "curly braces" is more favored in the U.S., while "brackets" is more widely used in British English.

What does the braces symbol mean in math?

Braces are used. 1. To denote grouping of mathematical terms, usually as the outermost delimiter in a complex expression such as , 2.

What are {} used for?

In writing, curly brackets or braces are used to indicate that certain words and/or sentences should be looked at as a group.


3 Answers

Console.WriteLine("Substring: {0}", substring)

Is the same as

Console.WriteLine("Substring: " & substring)

When using Console.WriteLine, {n} will insert the nth argument into the string, then write it.

A more complex example can be seen here:

Console.WriteLine("{0} {1}{2}", "Stack", "Over", "flow")

It will print Stack Overflow.

like image 113
tckmn Avatar answered Oct 04 '22 11:10

tckmn


Console.WriteLine() and String.Format() use that syntax. It allows you to inject a variable into a string, for example:

dim name = "james"
String.Format("Hello {0}", name)

That string will be "Hello james"

Using Console.Writeline:

Console.WriteLine("Hello {0}",name)

That will write "Hello james"

like image 30
Lotok Avatar answered Oct 04 '22 13:10

Lotok


It's a placeholder. Beginning at the second parameter (substring in your case), they are included in the given string in the given order. This way you avoid long string concatenations using + operator and can do easier language localization, because you can pull the compete string including the placeholders to some external resource file etc.

like image 44
qqilihq Avatar answered Oct 04 '22 11:10

qqilihq