zzwin
asked on
How to split a string
There is an ABCD I would like to split and put into an array with elemets A,B,C,D. How to do this universally so next time if I have an ABCDE just to redim the array and get an array whose elements are A,B,C,D,E
ASKER
This doesn't work from some reason
Sub a()
Dim strarray() As String
stringvar = ABCD
For x = 0 To Len(stringvar)
strarray(x) = Mid(stringvar, x, 1)
MsgBox strarray(0)
Next
End Sub
Sub a()
Dim strarray() As String
stringvar = ABCD
For x = 0 To Len(stringvar)
strarray(x) = Mid(stringvar, x, 1)
MsgBox strarray(0)
Next
End Sub
Dim strarray() As String
stringvar = ABCD
For x = 0 To Len(stringvar)
strarray(x) = Mid(stringvar, x, 1)
MsgBox strarray(x) '<-- Changed this from 0 To x as your variable is x in your for next loop
Next
End Sub
Try that and let us know how it goes :)
I hope that helps you out :)
Kind regards
Gecko
stringvar = ABCD
For x = 0 To Len(stringvar)
strarray(x) = Mid(stringvar, x, 1)
MsgBox strarray(x) '<-- Changed this from 0 To x as your variable is x in your for next loop
Next
End Sub
Try that and let us know how it goes :)
I hope that helps you out :)
Kind regards
Gecko
You basically need what dr binks suggested :)
ASKER
Well, it will not work because the MsgBox will display whateveris requested. It seem the actual problem is somewhere here.
stringvar = ABCD
For x = 0 To Len(stringvar)
strarray(x) = Mid(stringvar, x, 1)
stringvar = ABCD
For x = 0 To Len(stringvar)
strarray(x) = Mid(stringvar, x, 1)
ASKER
It says Invalid call procedure or argument in the
strarray(x) = Mid(stringvar, x, 1)
strarray(x) = Mid(stringvar, x, 1)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sub a()
Dim strarray() As String
stringvar = "ABCD"
For x = 0 To Len(stringvar)
strarray(x) = Mid(stringvar, x, 1)
MsgBox strarray(0)
Next
End Sub
Did you try this?
Dim strarray() As String
stringvar = "ABCD"
For x = 0 To Len(stringvar)
strarray(x) = Mid(stringvar, x, 1)
MsgBox strarray(0)
Next
End Sub
Did you try this?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
hehe, I just tried the code, I found this works:
Dim strarray(1000) As String
stringvar = "ABCD"
For x = 1 To Len(stringvar)
strarray(x) = Mid(stringvar, x, 1)
MsgBox strarray(x)
Next
(forgot x has to start at 1 because there is no position 0 in a string, my bad :/ )
Dim strarray(1000) As String
stringvar = "ABCD"
For x = 1 To Len(stringvar)
strarray(x) = Mid(stringvar, x, 1)
MsgBox strarray(x)
Next
(forgot x has to start at 1 because there is no position 0 in a string, my bad :/ )
anyways, I have work tommorow and its getting quite late, im sure gecko_au2003 and Shauli can help you out from here zzwin.
I shall check in the morning to see if there was a solution and if not I shall help you out :)
nite
I shall check in the morning to see if there was a solution and if not I shall help you out :)
nite
ASKER
Just a question- what means
Dim strarray(1000)
Dim strarray(1000)
That means that you fix the size of the array to 1000 elements. There are two ways to define an array: Either you define the amount of elemnts before you load the array, as in Dim strarray(1000), or you extend the amount of elemnt while you add elemnts, as in:
Dim myArray() as string, Counter as integer
Redim Preserve myArray(Counter)
myArray(Counter) = anyvalue
Counter = Counter + 1
That way, you add an elemnt to the array "on the fly" instead fixing the size of the array.
S
Dim myArray() as string, Counter as integer
Redim Preserve myArray(Counter)
myArray(Counter) = anyvalue
Counter = Counter + 1
That way, you add an elemnt to the array "on the fly" instead fixing the size of the array.
S
ASKER
Thanx a lot guys.
To implement the "on the fly" option to the suggested code:
Dim strarray() As String, Counter As Integer
stringvar = "ABCD"
For x = 1 To Len(stringvar)
ReDim Preserve strarray(x)
strarray(x) = Mid(stringvar, x, 1)
Counter = Counter + 1
Next
S
Dim strarray() As String, Counter As Integer
stringvar = "ABCD"
For x = 1 To Len(stringvar)
ReDim Preserve strarray(x)
strarray(x) = Mid(stringvar, x, 1)
Counter = Counter + 1
Next
S
>>That means that you fix the size of the array to 1000 elements.<<
Actually no, it is 1001. Since an array defined as:
Dim strarray(1000) is zero based than that would be 1001 elements.
It is a common mistake, and is especially true in .NET
Actually no, it is 1001. Since an array defined as:
Dim strarray(1000) is zero based than that would be 1001 elements.
It is a common mistake, and is especially true in .NET
although it is a common mistake, in most other languages it would infact be only 1000 elements.
In general, acperkins would be correct. But not at this time, as if the asker chooses to go with Dim strarray(1000), and as the loop to add the elements starts from 1 (For x = 1 To Len(stringvar)), then in this case it is 1000 and not 1001.
S
S
>>But not at this time<<
Really? Than I am afraid to say, you have no clue. Please take the time to read up on arrays and in particular LBound and UBound. Here is a little exercise for you:
Dim strarray(1000)
Debug.Print "Elements in the array:"; UBound(strarray) - LBound(strarray) + 1
Really? Than I am afraid to say, you have no clue. Please take the time to read up on arrays and in particular LBound and UBound. Here is a little exercise for you:
Dim strarray(1000)
Debug.Print "Elements in the array:"; UBound(strarray) - LBound(strarray) + 1
Of course if you had done this:
Dim strarray(1 to 1000)
Or even this:
Option Base 1
Dim strarray(1000)
Then you would of course be correct (both of those arrays have only 1000 eleements), but then I would not be posting here then, would I?
Dim strarray(1 to 1000)
Or even this:
Option Base 1
Dim strarray(1000)
Then you would of course be correct (both of those arrays have only 1000 eleements), but then I would not be posting here then, would I?
--------------------------
dim strarray() as string
dim realarray() as string
for x=0 to len(stringvar)
strarray(x)=mid(stringvar,
next
redim realarray(x)
for y=0 to x
realarray(y) = strarray(y)
next
--------------------
of course if your string had a delimiter like:
A B C D
you could use the split() function like so:
------------------------
dim strarray() as string
dim realarray() as string
strarray() = split(stringvar," ")
redim realarray(ubound(strarray(
-------------------------
hope that helps