OOo Basic contains functions that convert other data types to a string. Although the Format function is the most versatile method of converting a number to a string, that level of control is typically not required. The Str function converts a string to a number with no localization, and the Val function converts it back to a number.
The Hex and Oct functions convert a Long to its corresponding hexadecimal or octal notation. The leading "&H" and "&O" are not included. To convert back to a number, these strings must be manually prepended to the string.
The CStr function is able to intelligently convert almost any data type to a string in a locale-specific way (see Table 7 ). The Str function is limited to numbers and does not perform a locale-specific conversion.
Type | Converted to String |
---|---|
Boolean | True or False |
Date | Formatted date string such as 06/08/2003 |
Null, uninitialized object | Run-time error |
Empty, uninitialized variant | Zero-length string |
any numeric value | Number as a string |
The CStr function is useful when you need to explicitly convert a value to a string to avoid incorrect default conversions to other types. For example, the first operand to the addition operator determines if the result is a string or a number. This is also an argument against the use of the addition operator (+) for string concatenation rather than the operator specifically designed for string concatenation (&).
Print 3 + "4" '7 Print CStr(3) + "4" '34
The Join function concatenates all of the elements in a one-dimensional array into a single string. If no delimiter is specified, a space separates each element.
Print Join(Array(3, 4, 5)) '3 4 5 Print Join(Array(3, 4, 5), "X") '3X4X5
The Split function is used to split a string into pieces based on an optional delimiter. This is essentially the opposite of the Join function and is the fastest method to parse a string into a series of substrings based on a delimiter.
Split("3 4 5") 'returns the array (3, 4, 5) Split("3X4X5", "X") 'returns the array (3, 4, 5)