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
zzwinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dr_binksCommented:
the way I would do it is:

----------------------------

dim strarray() as string
dim realarray() as string

for x=0 to len(stringvar)
   strarray(x)=mid(stringvar,x,1)
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

0
zzwinAuthor Commented:
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
0
Shane Russell2nd Line Desktop SupportCommented:
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Shane Russell2nd Line Desktop SupportCommented:
You basically need what dr binks suggested :)
0
zzwinAuthor Commented:
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)
0
zzwinAuthor Commented:
It says Invalid call procedure or argument in the

strarray(x) = Mid(stringvar, x, 1)
0
dr_binksCommented:
stringvar = ABCD
For x = 0 To Len(stringvar)
   strarray(x) = Mid(stringvar, x, 1)

I dont know if that was a lypo... but it should be:

stringvar = "ABCD"   << you need the " " other wise it takes the value out of the variable called 'ABCD'
For x = 0 To Len(stringvar)
   strarray(x) = Mid(stringvar, x, 1)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zzwinAuthor Commented:
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?
0
ShauliCommented:
dr_binks :
It will still say Invalid call procedure or argument and that is because you begin the loop from 0 (zero) instead of 1 (one).
Mid cannot accept 0 (zero) as argument. So your loop should be:

For x = 1 To Len(stringvar)


S
0
dr_binksCommented:
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 :/ )
0
dr_binksCommented:
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
0
zzwinAuthor Commented:
Just a question- what means
 Dim strarray(1000)
0
ShauliCommented:
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
0
zzwinAuthor Commented:
Thanx a lot guys.
0
ShauliCommented:
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
0
Anthony PerkinsCommented:
>>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
0
dr_binksCommented:
although it is a common mistake, in most other languages it would infact be only 1000 elements.
0
ShauliCommented:
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
0
Anthony PerkinsCommented:
>>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
0
Anthony PerkinsCommented:
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?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.