Export (0) Print
Expand All

Strings.Format Method

Returns a string formatted according to instructions contained in a format String expression.

Namespace: Microsoft.VisualBasic
Assembly: Microsoft.VisualBasic (in microsoft.visualbasic.dll)

'Declaration
Public Shared Function Format ( _
	Expression As Object, _
	<OptionalAttribute> Optional Style As String = "" _
) As String
'Usage
Dim Expression As Object
Dim Style As String
Dim returnValue As String

returnValue = Strings.Format(Expression, Style)
public static String Format (
	Object Expression, 
	/** @attribute OptionalAttribute() */ String Style
)
public static function Format (
	Expression : Object, 
	Style : String
) : String

Parameters

Expression

Required. Any valid expression.

Style

Optional. A valid named or user-defined format String expression.

Return Value

Returns a string formatted according to instructions contained in a format String expression.

For more detailed information, see the Visual Basic topic Format Function.

If you are formatting a nonlocalized numeric string, you should use a user-defined numeric format to ensure that you get the look you want.

The System.String.Format method also provides similar functionality.

For information on how to create the Style argument, see the appropriate topic listed below:

To format

Do this

Numbers

Use predefined numeric formats or create user-defined numeric formats.

Dates and times

Use predefined date/time formats or create user-defined date/time formats.

Date and time serial numbers

Use date and time formats or numeric formats.

If you try to format a number without specifying Style, the Format function provides functionality similar to the Str function, although it is internationally aware. However, positive numbers formatted as strings using the Format function don't include a leading space reserved for the sign of the value; those converted using the Str function retain the leading space.

This example shows various uses of the Format function to format values using both String formats and user-defined formats. For the date separator (/), time separator (:), and the AM/PM indicators (t and tt), the actual formatted output displayed by your system depends on the locale settings the code is using. When times and dates are displayed in the development environment, the short time format and short date format of the code locale are used.

NoteNote

For locales that use a 24-hour clock, the AM/PM indicators (t and tt) display nothing.

Dim TestDateTime As Date = #1/27/2001 5:04:23 PM#
Dim TestStr As String
' Returns current system time in the system-defined long time format.
TestStr = Format(Now(), "Long Time")
' Returns current system date in the system-defined long date format.
TestStr = Format(Now(), "Long Date")
' Also returns current system date in the system-defined long date 
' format, using the single letter code for the format.
TestStr = Format(Now(), "D")

' Returns the value of TestDateTime in user-defined date/time formats.
' Returns "5:4:23".
TestStr = Format(TestDateTime, "h:m:s")
' Returns "05:04:23 PM".
TestStr = Format(TestDateTime, "hh:mm:ss tt")
' Returns "Saturday, Jan 27 2001".
TestStr = Format(TestDateTime, "dddd, MMM d yyyy")
' Returns "17:04:23".
TestStr = Format(TestDateTime, "HH:mm:ss")
' Returns "23".
TestStr = Format(23)

' User-defined numeric formats.
' Returns "5,459.40".
TestStr = Format(5459.4, "##,##0.00")
' Returns "334.90".
TestStr = Format(334.9, "###0.00")
' Returns "500.00%".
TestStr = Format(5, "0.00%")

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft