Format() allows
you to build a composite string by replacing format tags within a string with a variable number
of parameters passed in. Let??™s look at a quick example of string format usage:
Imports System
Imports System.Globalization
Imports System.Windows.Forms
Public Class EntryPoint
Shared Sub Main(ByVal args As String())
If args.Length < 3 Then
Console.WriteLine("Please provide 3 parameters")
Return
End If
CHAPTER 9 n WORKING WITH STRINGS 174
Dim composite As String = _
String.Format("{0}, {1}, and {2}.", args(0), args(1), args(2))
Console.WriteLine(composite)
End Sub
End Class
Here are the results from the previous example:
Jack, Jill, and Spot.
nNote To run this example, you must create a command-line argument in your project properties. On the
Debug tab, add ???Jack??? ???Jill??? ???Spot??? (including the double quotes) in the Start Options area.
A placeholder is delimited by braces and the number within it is the zero-based index to
the following parameter list. The String.Format method, as well as the Console.WriteLine
method, has an overload that accepts a variable number of parameters to use as the replacement
values. In this example, the String.Format method replaces each placeholder using the
general formatting of the type that you can get via a call to the parameter-less version of
ToString().
Pages:
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296