Split string into array

gauntletxg

Well-known Member
Joined
Jul 15, 2008
Messages
636
Say I have a string, "a test array." I want to split this into an array where each character is an element. I was thinking something like

Code:
Dim MyArray As Variant
Dim MyString As String
MyString = "a test array"
MyArray = Split(MyString, "", Len(MyString), vbTextCompare)

But this doesn't work because Split() returns the entire string when the delimiter is a zero-length string. How would I go about doing this?

Thanks.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Why do I always forget about Mid?

Code:
Function StringToArray(MyString As String) As Variant
Dim MyArray() As Variant

Dim i, iMax As Integer
iMax = Len(MyString)
ReDim MyArray(1 To iMax)
For i = 1 To iMax
    MyArray(i) = Mid(MyString, i, 1)
Next i
StringToArray = MyArray
End Function
Suggestions anyone?
 
Upvote 0
I was just thinking about this last week, perhaps from a similar post. To the best of my knowledge, VBA does not have an inbuilt function to slice a string (VB does, or at least VB.NET does). Although I may be mistaken. Here's one way. Does anyone have a better?

Code:
Sub TestIt()
Dim a() As String
Dim s As String

    s = "a test array"
    Call Split_String(a, s)
    Call TestOutput(a)

End Sub
'----------------------------------------------------
Sub Split_String(ByRef arrStringArray() As String, _
    ByVal strText As String)
Dim i As Long

    ReDim arrStringArray(Len(strText) - 1)
    For i = 0 To Len(strText) - 1
        arrStringArray(i) = Mid(strText, i + 1, 1)
    Next i

End Sub
'----------------------------------------------------
Sub TestOutput(ByRef a() As String)
Dim i As Long
    
    For i = 0 To UBound(a)
        Debug.Print i & ":" & String(5 - Len(CStr(i)), " ") & a(i)
    Next i

End Sub
 
Upvote 0
I see you've nearly done the same thing.

So, if we have
a() = StringToArray
and in StringToArray we have StringToArray() = MyArray(),
are all of these variables pointing to the same memory location?

I would think "yes" - I may have gone a little too carefully trying to avoid using extra memory (passing the variable explicitly and using it "ByRef").

--AB
 
Upvote 0
try
Code:
Sub test()
Dim MyArray As Variant
Dim MyString As String
MyString = "a test array"
MyArray = mySplit(MyString, "", Len(MyString), vbTextCompare)
MsgBox Join(MyArray, vbLf)
End Sub
 
Function mySplit(txt As String)
Dim i As Long, a() As String, n As Long
ReDim a(1 To Len(txt))
For i = 1 To Len(txt)
    If (Mid$(txt, i, 1) <> " ") + (Mid$(txt, i, 1) <> "") Then
        n = n + 1 : a(n) = Mid$(txt, i, 1)
    End If
Next
ReDim Preserve a(1 To n)
mySplit = a
End Function
 
Upvote 0
I believe this edit is needed in Jindon's solution:

Change:
Code:
MyArray = mySplit(MyString, "", Len(MyString), vbTextCompare)

To:
Code:
MyArray = mySplit(MyString)

As usual, very nice code. --AB
 
Upvote 0
Here's my solution:

Code:
Public Function StringToCharArray(ByRef sIn As String) As String()

    StringToCharArray = Split(StrConv(sIn, vbUnicode), Chr(0))

End Function
No iteration required :)

Here's a test harness if you want to try it out yourself

Code:
Sub Test()
    
    Const TEST_STRING = "Hello World!"
    
    Dim a() As String
    Dim check As String
    
    'Convert string into char array
    a = StringToCharArray(TEST_STRING)
    
    'Join array back together
    check = Join(a, vbNullString)
    
    'Verify that no data was lost in the conversion
    Debug.Print "a() and input string are equivalent:", TEST_STRING = check
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,038
Messages
6,122,798
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top