ImportC "crtdll.lib"
wcsstr.i (*str1, *str2)
EndImport
Procedure.i Split_wcsstr(Array StringArray.s(1), StringToSplit.s, Separator.s = " ")
Protected c = CountString(StringToSplit, Separator)
; We have to have a string to split
If Len(StringToSplit) = 0
ProcedureReturn -1
EndIf
; We return back the string as is if no separator found
If c = 0
ReDim StringArray(0)
StringArray(0) = StringToSplit
ProcedureReturn ArraySize(StringArray()) + 1
EndIf
ReDim StringArray(c)
Define *StringToSplit = @StringToSplit
Define *pfound = wcsstr(*StringToSplit, @Separator)
Define.i i
While *pfound
StringArray(i) = PeekS(*StringToSplit, (*pfound - *StringToSplit) / 2)
*StringToSplit = *pfound + 2
*pfound = wcsstr(*StringToSplit, @Separator)
i + 1
Wend
StringArray(i) = PeekS(*StringToSplit)
ProcedureReturn c + 1
EndProcedure
The separator is any character from the specified set. The separator is not an entire string. If the separator is repeated in the line under study, then it is considered as a single separator, that is, empty elements are not added.
Not at computer but as a general rule pass in strings by address and as strings are null terminated theres no need to check the length. Just loop until null That way you eliminate a parse of the string to get the length for the copy and also a second parse to get the length. For the loop.
Procedure StringField_List(*source,List StringFields.s(),separator=' ')
Protected *inp.Character
ClearList(StringFields())
If *source
*inp = *source
While *inp\c <> 0
While (*inp\c > separator )
*inp+2
Wend
AddElement(StringFields())
StringFields()= PeekS(*source,(*inp-*source)>>1)
If *inp\c <> 0
While *inp\c <= separator
*inp+2
*source = *inp
Wend
Else
Break
EndIf
Wend
EndIf
EndProcedure
Define S.s = "This is a test string to see if split and join are working."
NewList strings.s()
StringField_List(@S,Strings())
ForEach strings()
Debug Strings()
Next
idle wrote: ↑Mon Jun 13, 2022 4:24 am
Try this its not exactly the same as it skips anything below the separator character
I had such an idea, given that there are unreadable characters below the space, but I wanted universality, since in my program the user specifies which character he will use. It can be a comma or a custom unicode character in the form of some kind of shape.