Page 1 of 1
[DONE] Some way to split a string into an array?
Posted: Sat Apr 03, 2010 5:39 pm
by PresFox
Hello,
Next question (im making seperate threads to increase searchability for others, please let me know if this is frowned upon)
I have a string wich consists of several values, split by a delimiter, eg: ABC*DEFG*JKL
I want to split this in 3 different values, ABC, DEFG, and JKL.
In PHP ( wich i do know), i could explode this to an array, but how would i do this in PB?
Is using FindString and MID the only way? or is there a explode like function?
Thanks in advance!
Re: Explode? Some way to split a string into an array?
Posted: Sat Apr 03, 2010 5:48 pm
by ts-soft
Re: Explode? Some way to split a string into an array?
Posted: Sat Apr 03, 2010 6:00 pm
by Demivec
PresFox wrote:Is using FindString and MID the only way? or is there a explode like function?
CountString() and StringField() should do the trick.
Code: Select all
Procedure explodeStringArray(Array a$(1), s$, delimeter$)
Protected count, i
count = CountString(s$,delimeter$) + 1
Debug Str(count) + " substrings found"
Dim a$(count)
For i = 1 To count
a$(i - 1) = StringField(s$,i,delimeter$)
Next
ProcedureReturn count ;return count of substrings
EndProcedure
Dim output.s(0) ;this will be resized later
explodeStringArray(output(), "ABC*DEFG*JKL", "*")
For i = 0 To ArraySize(output())
Debug output(i)
Next
I likewise second ts-soft's suggestion to peruse the manual.

Re: Explode? Some way to split a string into an array?
Posted: Sat Apr 03, 2010 6:05 pm
by Michael Vogel
Not sure, if StringField is the wanted "solution", it works fine and simple but costs execution time for each call.
Let's say, PresFox wants to load a textfile and do a lot of line per line manipulations within this text - then an "explode" would be nice, if the single string would be transformed to an array (but only, if an implode would also exist

)
Michael
[EDIT] Demivec's code demonstrate how such an explode could work - the only thing to be aware is that (large) strings are kept two times in the memory here
Re: Explode? Some way to split a string into an array?
Posted: Sat Apr 03, 2010 6:40 pm
by PresFox
Demenivec, thanks, works wonders!

Re: Explode? Some way to split a string into an array?
Posted: Sat Apr 03, 2010 6:43 pm
by Demivec
PresFox wrote:Demenivec, thanks, works wonders!

Your welcome. It is only one of the ways (and not necessarily the best way) to accomplish the task.

Re: Explode? Some way to split a string into an array?
Posted: Sat Apr 03, 2010 10:46 pm
by UserOfPure
Another variation, which takes into account multiple asterisks and doesn't include them. (Demivec forgot to check that, try his code with "*ABC*****DEFG***JKL*").
Code: Select all
Dim a$(100)
s$="*ABC*****DEFG***JKL*"
Repeat
f=FindString(s$,"*",1)
If f
t$=Left(s$,f-1)
If t$<>"" : c+1 : a$(c)=t$ : EndIf
s$=Mid(s$,f+1)
EndIf
Until f=0
If s$<>"" : c+1 : a$(c)=s$ : EndIf
Debug Str(c)+" substrings found"
For a=1 To c
Debug a$(a)
Next
Re: [DONE] Some way to split a string into an array?
Posted: Fri Dec 30, 2016 11:05 pm
by xakep
Sorry for bringing the life to this topic, in the future others may need the same function.
There is my more "advanced" version:
Code: Select all
Procedure.l GetArray_Between(String.s, Left.s, Right.s, Array InArray.s(1))
Define LenString.l, LeftMarker.l, RightMarker.l, NewString.s, LeftL.l, iCount.l, MaxLoops.l, cLoop.l
MaxLoops = 99 ;Just to be safe
Repeat
cLoop + 1
If NewString
String = NewString
EndIf
LenString = Len(String)
LeftL = Len(Left)
If LenString And LeftL
LeftMarker = FindString(String, Left)
If LeftMarker>0
RightMarker = FindString(String, Right, LeftMarker+1)
If RightMarker>0
InArray(iCount) = Mid(String, LeftMarker+LeftL, (RightMarker-LeftMarker)-LeftL)
NewString = Right(String, LenString - RightMarker)
If Len(NewString) < (LeftL * 2) ;To be extra safe =)
NewString = ""
Else
iCount + 1
ReDim InArray(iCount)
EndIf
Else
FreeArray(InArray())
EndIf
Else
FreeArray(InArray())
EndIf
Else
FreeArray(InArray())
EndIf
Until NewString = "" Or cLoop => MaxLoops
EndProcedure
;Test
Define MyStr.s, i.i
;MyStr = "(a)(b)(c)"
;MyStr = "[[a]][[b]][[c]]"
;MyStr = "{*a1*}{*a2*}{*a3*}{*a79*}"
MyStr = "$#a11}$#b2345}$#c157}$#d this works too}"
Dim MyOutput.s(0)
GetArray_Between( MyStr, "$#", "}", MyOutput())
For i = 0 To ArraySize(MyOutput())
Debug MyOutput(i)
Next