8 out of 17 rated this helpful Rate this topic

CStr Function

Windows Scripting 5.8

Updated: August 2009

Returns an expression that has been converted to a Variant of subtype String.


                      CStr(expression)

The expression argument is any valid expression.

Use the CStr function to provide conversions from any data type to a String subtype. CStr forces the results to be expressed as a String.

The CStr function uses the locale setting of your system to determine how to perform conversions. Different decimal separators are properly recognized, depending on the locale setting.

The data in expression determines what is returned according to the following table:

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.

Null

A run-time error.

Empty

A zero-length String ("").

Error

A String containing the word Error followed by the error number.

Other numeric

A String containing the number.

The following example uses the CStr function to convert a numeric value to a String:

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

Date

History

Reason

August 2009

Modified remarks.

Customer feedback.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Strange behavior converting String to String

In the following example the result is "Test 1: true" and "Test 2: false"

<html>
<head>
<title>Cast Test</title>
<script type="text/vbscript">
if clng(2)="2" then
 alert("Test 1: true")
else
 alert("Test 1: false")
end if
if clng(2)=cstr("2") then
 alert("Test 2: true")
else
 alert("Test 2: false")
end if
</script>
</head>
<body>
</body>
</html>