Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

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
0
zzwin
Asked:
zzwin
  • 6
  • 5
  • 4
  • +2
2 Solutions
 
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
 
gecko_au2003Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
gecko_au2003Commented:
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 5
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now