What numbers in braces, for example. "{0}" means?

I searched around, but with great difficulty found the answer to this question, because what I'm looking for is so non-specific.

I saw a lot of code that uses {0} in it, and I still can't figure out what it does. Here is an example:

 Dim literal As String = "CatDogFence" Dim substring As String = literal.Substring(6) Console.WriteLine("Substring: {0}", substring) 
+4
source share
4 answers
 Console.WriteLine("Substring: {0}", substring) 

Same as

 Console.WriteLine("Substring: " & substring) 

When using Console.WriteLine , {n} inserts the argument n th into the string and then writes it.

A more complex example can be seen here:

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

He will print Stack Overflow .

+4
source

Console.WriteLine () and String.Format () use this syntax. It allows you to enter a variable in a string, for example:

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

This line will be "Hello james"

Using Console.Writeline:

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

This will write "Hello james"

+2
source

This is a placeholder. Starting from the second parameter (a substring in your case), they are included in the given line in this order. This way you avoid long string concatenations with the + operator and can simplify the localization of the language, since you can pull a competing string, including placeholders, into some external resource file, etc.

+1
source

It is called compound formatting and is supported in many ways; Console.WriteLine is one. In addition to indexed placeholders, there are other available features. Here is a link to the documentation that shows some other features of composite formatting.

Composite formatting

0
source

Source: https://habr.com/ru/post/1483937/


All Articles