split string into array/list based on delimiter?
split string into array/list based on delimiter?
Hello again,
In my adventures of transitioning to pure basic I have been looking for a way to perform this task.
In basics, I want to be able to split a string into a list, or an array (probably a list) based on a delimiter found in the string.
I am aware string field can be used, but what if you don't know the exact contents of the string that you will be dealing with? For example parsing user/map data.
so let's say I have a string that has several words split by commas. Like, cat,dog,mouse,bird,keyboard.
How would I split this string so that each word would be a new entry in this list/array?
Thank you all for your time!
In my adventures of transitioning to pure basic I have been looking for a way to perform this task.
In basics, I want to be able to split a string into a list, or an array (probably a list) based on a delimiter found in the string.
I am aware string field can be used, but what if you don't know the exact contents of the string that you will be dealing with? For example parsing user/map data.
so let's say I have a string that has several words split by commas. Like, cat,dog,mouse,bird,keyboard.
How would I split this string so that each word would be a new entry in this list/array?
Thank you all for your time!
Re: split string into array/list based on delimiter?
https://github.com/SicroAtGIT/PureBasic ... oArray.pbi
Code: Select all
IncludeFile "ExplodeStringIntoArray.pbi"
Define CountOfItems, i
Dim Items$(0)
CountOfItems = ExplodeStringIntoArray(",", "cat,dog,mouse,bird,keyboard", Items$())
For i = 0 To CountOfItems
Debug Items$(i)
Next

Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Re: split string into array/list based on delimiter?
Sicro was faster.
Here is a quick example:
You could also split to Linked List instead of Array, and you could include commas in your data by using doublequotes or backslash-escapes, you could return the array size, etc. etc. etc.
Here is a quick example:
Code: Select all
Procedure.i SplitString(String.s, Delimiter.s, Array Output.s(1))
n = 1 + CountString(String, Delimiter)
Dim Output.s(n - 1)
For i = 0 To n-1
Output(i) = Trim(StringField(String, 1+i, Delimiter))
Next i
EndProcedure
Dim Word.s(0)
SplitString("cat,dog,mouse,bird,keyboard", ",", Word())
For i = 0 To ArraySize(Word())
Debug Word(i)
Next i
Re: split string into array/list based on delimiter?
Wow guys! Thanks so much!
As I am still learning, I will use both examples and see which one works best.
Again, thanks for your time!
As I am still learning, I will use both examples and see which one works best.
Again, thanks for your time!
Re: split string into array/list based on delimiter?
my two cents:
Code: Select all
Procedure SplitString(string$,delimiter$,Array a.s(1))
If CreateRegularExpression(0,"[^"+delimiter$+"]+")
ProcedureReturn ExtractRegularExpression(0,string$,a())
EndIf
EndProcedure
Dim Word.s(0)
SplitString("cat,dog,mouse,bird,keyboard", ",", Word())
For i=0 To ArraySize(Word())
Debug Word(i)
Next
there is no sig, only zuul (and the following disclaimer)
WARNING: may be talking out of his hat
WARNING: may be talking out of his hat
Re: split string into array/list based on delimiter?
@citystate: This is a quick solution, but be careful with special characters like "." or "*" as delimiters since they have a special meaning in RegEx and need to be escaped.
Good morning, that's a nice tnetennba!
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Re: split string into array/list based on delimiter?
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: split string into array/list based on delimiter?
Should be optimized fast native command imho; split and join.
Re: split string into array/list based on delimiter?
FYI, regex is 1000x slower than optimized PB code. Use of StringField() is also slow.
You must choose when you need speed vs simplicity.
You must choose when you need speed vs simplicity.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum