Reputation: 23901
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:
Upvotes: 4
Views: 7947
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
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
Reputation: 5376
"Pattern" is part of a "Regular Expression."
See here for starters: http://www.regular-expressions.info/dotnet.html
Upvotes: 0