Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

seperating a delimited string into a dynamic array

Status
Not open for further replies.

TheSorceror

Programmer
Jul 29, 2010
11
US
I am looking for a piece of code to help me seperate a comma delimited string into a dynamic array.

For example:
inputting a string of comma delimeted characters into a edit box on a form then seperate the string into the sepperated characters.
text entered equals "abcd,gfskfs,asdfwe,llooll,foasld"
this would then need to be entered into a dynamic array of type string as seperate strings.
ie
abcd
gfskfs
asdfwe
llooll
foasld

Problem the delimited text string can be of an unknown length so the string array would need to be redimmed.

How do i seperate the delimited string into seperate strings???

This can be done using the string class in VB but does Extra support the string class?
 

Hi,

If you were using a better BASIC than Extra has, like Excel VBA, you would have the Split function.

So here's a function you can add to your Excta VB...
Code:
Function SplitIt(s As String, delim As String)
  Dim i As Integer, i1 As Integer, b As String, a() As String, idx As Integer

  i1 = 1
  For i = 1 To Len(s)
    b = Mid(s, i, Len(delim))
    If b = delim Then
        GoSub GrabIt
    End If
  Next
  GoSub GrabIt
  SplitIt = a
  Exit Function
GrabIt:
  ReDim Preserve a(idx)
  a(idx) = Mid(s, i1, i - i1)
  idx = idx + 1
  i1 = i + Len(delim)
  Return
End Function
the function returns an array of each value that is separated by your specified delimiter.


Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Thanks Skip, Not a very easy way of doing it but i will give it a whirl.

So i would call this like

stringArray() = SplitIt "abcd,gfskfs,asdfwe,llooll,foasld" , ","

Naturally i would not be able to Dim stringArray() or it would only have a max size of 8 elements. or i would have to dim it to an impossibly large size and hope the text entered does not exceed its max size giving me an out of bounds error.
 


Code:
dim YourString as string, a as variant, i as integer
YourString = "abcd,gfskfs,asdfwe,llooll,foasld"
a = SplitIt(YourString, ",")
for i = lbound(a) to ubound(a)
 msgbox a(i)
next
or even this
Code:
dim YourString as string, i as integer
YourString = "abcd,gfskfs,asdfwe,llooll,foasld"
for i = lbound(SplitIt(YourString, ",")) to ubound(SplitIt(YourString, ","))
 msgbox SplitIt(YourString, ",")(i)
next


Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Hmm unfortunately the SplitIt function above does not compile. :( it complaines about improper use of reserved word GoSub... :(

I will have to think of a work around.

Geez Extra Basic is soo Primitive.
 


I do ALL my Extra manipulation from Excel VBA, since my objective is always data collection from IMS to Excel, or updating IMS from an Excel list.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Well the work around i got is just repeating the code. once in the loop then again outside of the loop.. Messy i know. but it compiles OK

Function SplitIt(s as String, delim As String)

Dim i As Integer, i1 As Integer, b as String, a() As String, idx As Integer

i1 = 1
For i = 1 to Len(s)
b = Mid(s,i,Len(delim))
If b = delim then
ReDim Preserve a(idx)
a(idx) = Mid(s, i1, i - i1)
idx = idx + 1
i1 = i + Len(delim)
End If
Next
ReDim Preserve a(idx)
a(idx) = Mid(s, i1, i - i1)
idx = idx + 1
i1 = i + Len(delim)
SplitIt = a

End Function

The next issue is trying to get the call to the function working. In the first exaple you give,

dim YourString as string, a as variant, i as integer
YourString = "abcd,gfskfs,asdfwe,llooll,foasld"
a = SplitIt(YourString, ",")
for i = lbound(a) to ubound(a)
msgbox a(i)
next

It reports a syntax error . Incorrect or missing Subscript for array a.

The second example complains that SplitIt is not an array. so guess that will not work at all.. I think the first is probably more logically correct.
 
Hehe I managed to Get it working if i Declare a() as a Global Variable tho and treat The SplitIt function as one that does not return a Variable.

Although i really hate using global variables.. I think i will have to take what Works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top