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!
[DONE] Some way to split a string into an array?
[DONE] Some way to split a string into an array?
Last edited by PresFox on Sat Apr 03, 2010 6:40 pm, edited 1 time in total.
User of PB 4.41 - x86
On Windows 7
Intel Core I5 2.26GHZ
8 GB RAM
ATI Mobility Radeon HD 5650
On Windows 7
Intel Core I5 2.26GHZ
8 GB RAM
ATI Mobility Radeon HD 5650
Re: Explode? Some way to split a string into an array?
CountString() and StringField() should do the trick.PresFox wrote:Is using FindString and MID the only way? or is there a explode like function?
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
- Michael Vogel
- Addict
- Posts: 2677
- Joined: Thu Feb 09, 2006 11:27 pm
- Contact:
Re: Explode? Some way to split a string into an array?
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
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?
Demenivec, thanks, works wonders!
User of PB 4.41 - x86
On Windows 7
Intel Core I5 2.26GHZ
8 GB RAM
ATI Mobility Radeon HD 5650
On Windows 7
Intel Core I5 2.26GHZ
8 GB RAM
ATI Mobility Radeon HD 5650
Re: Explode? Some way to split a string into an array?
Your welcome. It is only one of the ways (and not necessarily the best way) to accomplish the task.PresFox wrote:Demenivec, thanks, works wonders!
-
- Enthusiast
- Posts: 469
- Joined: Sun Mar 16, 2008 9:18 am
Re: Explode? Some way to split a string into an array?
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?
Sorry for bringing the life to this topic, in the future others may need the same function.
There is my more "advanced" version:
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