傳回字串,其格式化方式是根據格式 String 運算式內包含的指令。
命名空間: Microsoft.VisualBasic
組件: Microsoft.VisualBasic (在 microsoft.visualbasic.dll 中)
語法
'宣告
Public Shared Function Format ( _
Expression As Object, _
<OptionalAttribute> Optional Style As String = "" _
) As String
'用途
Dim Expression As Object
Dim Style As String
Dim returnValue As String
returnValue = Strings.Format(Expression, Style)
public static string Format (
Object Expression,
[OptionalAttribute] string Style
)
public:
static String^ Format (
Object^ Expression,
[OptionalAttribute] String^ Style
)
public static String Format (
Object Expression,
/** @attribute OptionalAttribute() */ String Style
)
public static function Format (
Expression : Object,
Style : String
) : String
參數
- Expression
必要項。任何有效的運算式。
- Style
選擇項。有效的具名或使用者定義的格式 String 運算式。
傳回值
傳回字串,其格式化方式是根據格式 String 運算式內包含的指令。
備註
如需詳細資訊,請參閱 Visual Basic 的主題 Format 函式。
如果您正要格式化未當地語系化的數值字串,您應該使用使用者定義的數值格式,確保您可以得到想要的格式。
System.String.Format 方法也會提供類似的功能。
如需如何建立 Style 引數的詳細資訊,請參閱下列的適當主題:
若要格式化 |
請執行 |
|---|---|
數字 |
|
日期和時間 |
|
日期與時間序號 |
使用日期與時間格式或數值格式。 |
如果您嘗試在不指定 Style 的情況下將數字格式化,則 Format 函式會提供與 Str 函式類似的功能 (雖然它具有國際感知)。但是,使用 Format 函式格式化成字串的正數不會包括前置空格 (保留給此值的正負號使用);使用 Str 函式轉換的數字則會保留前置空格。
範例
此範例將示範 Format 函式的各種使用方式,以透過 String 格式及使用者定義的格式,將值格式化。對於日期分隔符號 (/)、時間分隔符號 (:) 和 AM/PM 指示器 (t 和 tt) 而言,系統顯示的實際格式化輸出需視程式碼使用的地區設定而定。當時間和日期顯示在開發環境內時,會使用程式碼地區設定的簡短時間格式和簡短日期格式。
注意事項 |
|---|
若為使用 24 小時制的地區設定,AM/PM 指示器 (t 和 tt) 不會顯示任何內容。 |
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
.NET Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱系統需求一節的內容。
版本資訊
.NET Framework
支援版本:2.0、1.1、1.0
.NET Compact Framework
支援版本:2.0、1.0
請參閱
參考
Strings 類別
Strings 成員
Microsoft.VisualBasic 命名空間
Format
其他資源
Format 函式
字串操作摘要
不同數值的不同格式 (Format 函式)
預先定義的日期/時間格式 (Format 函式)
預先定義的數值格式 (Format 函式)
Str 函式
型別轉換函式
使用者定義日期/時間格式 (Format 函式)
使用者定義數值格式 (Format 函式)
注意事項