CStr Function
Returns an expression that has been converted to a Variant of subtype String.
Syntax
CStr(expression)
Remarks
The expression argument is any valid expression.
In general, you can document your code using the data type conversion functions to
show that the result of some operation should be expressed as a particular data type
rather than the default data type. For example, use CStr
to force
the result to be expressed as a String.
You should use the CStr
function instead of Str
to
provide internationally aware conversions from any other data type to a String
subtype. For example, different decimal separators are properly recognized depending
on the locale setting of your system.
The data in expression determines what is returned according to the following table:
Table 11-8 Expression Return Mapping
If expression is | CStr returns |
---|---|
Boolean | A String containing True or False. |
Date | A String containing a date in the short-date format of your system. |
Empty | A zero-length String (" "). |
Other numeric | A String containing the number. |
The following example uses the CStr
function to convert a numeric
value to a String:
Example 1:
Dim MyDouble, MyString
MyDouble = 437.324 ' MyDouble is a Double.
MyString = CStr(MyDouble) ' MyString contains "437.324".
'Output: "437.324"
Example 2:
Dim MyBool
MyBool = True ' MyBool is a Boolean.
MyString = CStr(MyBool) ' MyString contains "True".
'Output: "True"
Example 3:
MyBool = False ' MyBool is a Boolean.
MyString = CStr(MyBool) ' MyString contains "False".
'Output: "False"
Example 4:
Dim MyDate
MyDate = CDate("10/19/1962")
MyString = CStr(MyDate)
'Output: "19/10/1962" (or your system's short-date format)
Example 5:
Dim MyInt
MyInt = 100
MyString = CStr(MyInt)
'Output: "100"
Example 6:
Dim MyValue
MyValue = Empty
MyString = CStr(MyValue)
'Output: ""
Example 7:
Dim MyLong
MyLong = 1234567890
MyString = CStr(MyLong)
'Output: "1234567890"