CStr - VBScript


Overview

CStr is a VB Script command used to convert data to a string. This is particularly useful when working with data of various types and you need to ensure consistency in string operations.

Syntax

CStr(expression)

| Parameter | Description |
|—|—|
| expression | The value to be converted to a string. |

Options/Flags

None

Examples

Example 1: Converting a number to a string

Dim num = 123
Dim str = CStr(num)

Example 2: Converting a Boolean value to a string

Dim bool = True
Dim str = CStr(bool)

Example 3: Converting a date to a string

Dim dt = Date
Dim str = CStr(dt)

Common Issues

  • Type mismatch error: Occurs when attempting to convert a value that cannot be converted to a string. Ensure that the value is of a valid type for conversion.

Integration

CStr can be used in conjunction with other VB Script commands to create more complex string manipulation operations.

Dim num1 = 123, num2 = 456
Dim result = CStr(num1) & "-" & CStr(num2)
  • Val – Converts a string to a number.
  • Str – Converts a number or Boolean value to a string.
  • Format – Formats a number or date as a string.