CStr 函式

傳回已轉換為子類型 String 之變異的表示式。

語法

CStr(expression)

備註

表示式引數是任何有效的表示式。

一般而言,您可以使用資料類型轉換函式來記錄程式碼,以顯示某些作業的結果應該以特定的資料類型而非預設的資料類型來表示。例如,使用 CStr 強制將結果以字串表示。

您應該使用 CStr 函式 (而非 Str) 來提供從任何其他資料類型到 String 子類型的國際感知轉換。例如,根據您系統的地區設定,可以正確識別不同的小數分隔符號。

表示式中的資料會根據下表決定傳回的內容:

表格 11-8 表示式傳回對應

如果表示式為 CStr 傳回
布林值 包含 True 或 False 的字串
Date 包含系統簡短日期格式之日期的字串
Empty 長度為零的字串 (" ")。
其他數值 包含數字的字串

下列範例使用 CStr 函式將數值轉換成字串:

範例 1

Dim MyDouble, MyString
MyDouble = 437.324           ' MyDouble is a Double.
MyString = CStr(MyDouble)    ' MyString contains "437.324".
'Output: "437.324"

範例 2

Dim MyBool
MyBool = True                ' MyBool is a Boolean.
MyString = CStr(MyBool)      ' MyString contains "True".
'Output: "True"

範例 3

MyBool = False               ' MyBool is a Boolean.
MyString = CStr(MyBool)      ' MyString contains "False".
'Output: "False"

範例 4

Dim MyDate
MyDate = CDate("10/19/1962")      
MyString = CStr(MyDate)      
'Output: "19/10/1962" (or your system's short-date format)

範例 5

Dim MyInt
MyInt = 100           
MyString = CStr(MyInt)              
'Output: "100"

範例 6

Dim MyValue
MyValue = Empty
MyString = CStr(MyValue)            
'Output: ""

範例 7

Dim MyLong
MyLong = 1234567890                
MyString = CStr(MyLong)            
'Output: "1234567890"