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:
- 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)?
- What are the rules of when and how you can assign parameters into a string like this?
- Can anyone point me to the MSDN reference? What is this syntax/string trick called?
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.