Solved

# How to split a string

Posted on 2005-04-04
Medium Priority
203 Views
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
0
Question by:zzwin
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 6
• 5
• 4
• +2

LVL 5

Expert Comment

ID: 13702514
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

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

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

Author Comment

ID: 13702566
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

LVL 23

Expert Comment

ID: 13702595
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

LVL 23

Expert Comment

ID: 13702609
You basically need what dr binks suggested :)
0

Author Comment

ID: 13702629
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

Author Comment

ID: 13702660
It says Invalid call procedure or argument in the

strarray(x) = Mid(stringvar, x, 1)
0

LVL 5

Accepted Solution

dr_binks earned 1200 total points
ID: 13702662
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

Author Comment

ID: 13702689
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

LVL 19

Assisted Solution

Shauli earned 800 total points
ID: 13702703
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

LVL 5

Expert Comment

ID: 13702706
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

LVL 5

Expert Comment

ID: 13702763
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

Author Comment

ID: 13702799
Just a question- what means
Dim strarray(1000)
0

LVL 19

Expert Comment

ID: 13702881
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

Author Comment

ID: 13702936
Thanx a lot guys.
0

LVL 19

Expert Comment

ID: 13702944
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

LVL 75

Expert Comment

ID: 13704004
>>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

LVL 5

Expert Comment

ID: 13704751
although it is a common mistake, in most other languages it would infact be only 1000 elements.
0

LVL 19

Expert Comment

ID: 13706131
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

LVL 75

Expert Comment

ID: 13707173
>>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

LVL 75

Expert Comment

ID: 13707200
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asseâ€¦
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). Uâ€¦
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that aâ€¦
###### Suggested Courses
Course of the Month13 days, 4 hours left to enroll