I'm not sure why I want to do this, but it seems like I used to know how. Now I cant get it to work.

The code would be something like:

Public Sub FormattedString()

'*** I want to break the string up into an array of
'*** characters and reverse it.
Dim strArray() as String

strArray = ToArray(strString)

Msgbox "strArray: " & strArray

End Sub

Private Function ToArray(strString As String) As String()

Dim strArray() As String
Dim intPointer As Integer
Dim intArrayPointer as Integer
Dim strLen As Integer

intArrayPointer = 1
strLen = Len(Trim(strString))
Debug.Assert Len(Trim(strString)) < 255
For intPointer = strLen To 1 Step -1
strArray(intArrayPointer) = Mid(strString, intPointer, 1)
intArrayPointer = intArrayPointer + 1
Next ' intPointer

ToArray = strArray

End Function


I've tried several combinations of parentheses and I've done the Google search. None of the stuff I found worked.

Any printable suggestions? ;}

Recommended Answers

All 3 Replies

Hi, You can try this one

Public Sub FormattedString()
    Dim strArray() As String
    
    strArray = ToArray("This is the Sample String")

    'MsgBox "strArray: " & strArray

End Sub

Private Function ToArray(strString As String) As String()

    Dim strArray() As String
    Dim intPointer As Integer
    Dim intArrayPointer As Integer
    Dim strLen As Integer
    
    intArrayPointer = 0
    strLen = Len(Trim(strString))
    
    ReDim strArray(strLen - 1) As String
    Debug.Assert Len(Trim(strString)) < 255
    
    For intPointer = strLen - 1 To 0 Step -1
        strArray(intArrayPointer) = Mid(strString, intPointer + 1, 1)
        intArrayPointer = intArrayPointer + 1
    Next ' intPointer
    
    ToArray = strArray
    
End Function

Use ReDim to re dimension your array.

After using ToArray () function it returns array of string containing each character.

How did you display the return value?

Convert array to String then Show in Message Box (Concatenate all the strings)

Private Function ArrayToString(strArray() As String) As String
    Dim i As Integer
    Dim strTmp As String
    
    For i = 0 To UBound(strArray)
        strTmp = strTmp & strArray(i)
    Next
    ArrayToString = strTmp
End Function
'
'
'It Can be used as

    MsgBox "strArray: " & ArrayToString (strArray)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.