CorString: ToCharArray

ToCharArray

Creates and array of chars (Integers) from the specified string.



 Public Function ToCharArray(
	  ByRef s As String,
	  Optional ByRef StartIndex As Variant,
	  Optional ByRef Length As Variant ) As Integer ( )

Parameters

s
[ByRef] String. The string to create the chars from.
StartIndex
[ByRef] Optional. Variant. The start index in the string to begin converting to a char array. This is zero-based.
Length
[ByRef] Optional. Variant. The number of characters to convert to chars.

Return Values

Integer() -  An array containing the converted characters from the string.

Remarks

The StartIndex parameter is zero-based. That is, the index of the first character in the string s is zero.

If Length is zero, the returned array is empty and has a zero length. If s is an empty string (""), the returned array is empty and has a zero length.

Exceptions

Exception Condition
ArgumentOutOfRangeException StartIndex or Length is less than zero.
-or-
StartIndex plus Length is greater than the length of s.

Examples

The following example converts a substring within a string to an array of characters, then enumerates and displays the elements of the array.

Public Sub Main()
    Const Str As String = "012wxyz789"
    Dim Arr() As Integer
    
    Arr = CorString.ToCharArray(Str, 3, 4)
    
    Debug.Print CorString.Format("The letters in '{0}' are: '{1}'", Str, NewString(Arr))
    
    Debug.Print CorString.Format("Each letter in '{0}' is:", Str)
    
    Dim c As Variant
    For Each c In Arr
        Debug.Print ChrW$(c)
    Next
End Sub

' This code produces the following output.
'
'    The letters in '012wxyz789' are: 'wxyz'
'    Each letter in '012wxyz789' is:
'    w
'    x
'    y
'    z

See Also

Project CorLib Overview

Class CorString Overview