Code : Tout sélectionner
; Split() functions, Purebasic 4.0+
Macro CountArray(array)
( PeekL( @array-8 ) )
EndMacro
Procedure.l SplitArray(array.s(1), text.s, separator.s = ",") ; String to Array
Protected index.l, size.l = CountString(text, separator)
ReDim array.s(size)
For index = 0 To size
array(index) = StringField(text, index + 1, separator)
Next
ProcedureReturn size
EndProcedure
Procedure.s JoinArray(array.s(1), separator.s = ",") ; Array to String
Protected index.l, result.s, size.l = CountArray(array()) - 1
For index = 0 To size
result + array(index)
If (index < size)
result + separator
EndIf
Next
ProcedureReturn result
EndProcedure
Procedure.l SplitList(list.s(), text.s, separator.s = ",") ; String to List
Protected index.l, size.l = CountString(text, separator)
For index = 0 To size
If AddElement(list())
list() = StringField(text, index + 1, separator)
EndIf
Next
ProcedureReturn size
EndProcedure
Procedure.s JoinList(list.s(), separator.s = ",") ; List to String
Protected result.s, size.l = CountList(list()) - 1
ForEach list()
result + list()
If (ListIndex(list()) < size)
result + separator
EndIf
Next
ProcedureReturn result
EndProcedure
; Examples
string.s = "abc,defg,hi,jklmop,qrs,tuv,wxyz"
; string -> array -> string
Dim a.s(0)
size.l = SplitArray(a(), string, ",")
For i = 0 To size
Debug a(i)
Next
Debug JoinArray(a())
; string -> list -> string
NewList b.s()
If SplitList(b(), string)
ForEach b()
Debug b()
Next
EndIf
Debug JoinList(b())
;--