bernie2436
bernie2436

Reputation: 23901

What do brackets like this { } mean when used in strings in VB.net? Where can I find the details?

I have a about brackets like this {} in VB.net.

I keep seeing syntax like this in MSDN documentation and in VB.net tutorials.

  Dim pattern As String = "(\d{3})-(\d{3}-\d{4})"
  Dim input As String = "212-555-6666 906-932-1111 415-222-3333 425-888-9999"
  Dim matches As MatchCollection = Regex.Matches(input, pattern)
  For Each match As Match In matches
     Console.Write("Area Code:        {0}", match.Groups(1).Value)
  Next 

prints: Area Code: 212 Area Code: 906 Area Code: 415 Area Code: 425

it seems vaguely like "this is a string {0}" , variable prints "this is a string " & valueOfVariable.ToString

But I have a few questions about the details of how this actually works:

  1. Does VB.net automatically assume that the curly brackets are holding a string parameter or does it depend on context. I've only seen this syntax as part of Console.WriteLine (I googled special characters vb.net 4.0 without luck)?
  2. What are the rules of when and how you can assign parameters into a string like this?
  3. Can anyone point me to the MSDN reference? What is this syntax/string trick called?

Upvotes: 4

Views: 7947

Answers (3)

David M
David M

Reputation: 72870

The two are not actually connected, it depends on context as you say.

In the pattern variable, the regular expression syntax \d{3} means exactly 3 occurences of a digit (0-9).

In the Console.Write, this overload internally passes the string to string.Format, where the curly-bracketed entries are interpreted as placeholders for formatting. It is only when passed to string.Format (explicitly or implicitly) that they take on this specific meaning.

Upvotes: 7

Akash Kava
Akash Kava

Reputation: 39916

It is called String Formatting,

http://msdn.microsoft.com/en-us/library/system.string.format.aspx

It will accept any parameters and it will convert to its text representation based on the formatting specified, there are many ways to control how to format the given object (it can be string, number, date anything).

Everything in .Net is derived from object and object has a method called ToString, which will return its string representation. So if number or anything else is passed to Format, this method will call ToString and use its string representation along with other settings.

Most cases, this method will be used by some other high level methods, like in Console.WriteLine etc, but eventually they all do same.

Composite Formatting

http://msdn.microsoft.com/en-us/library/txafckwd.aspx

Standard Numeric Formats

http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

Custom Numeric Formats

http://msdn.microsoft.com/en-us/library/0c899ak8.aspx

You will have to read all related links in MSDN to get more information.

Upvotes: 7

Jimmy D
Jimmy D

Reputation: 5376

"Pattern" is part of a "Regular Expression."

See here for starters: http://www.regular-expressions.info/dotnet.html

Upvotes: 0

Related Questions