split string into array/list based on delimiter?

Just starting out? Need help? Post your questions and find answers here.
Omar1993
New User
New User
Posts: 7
Joined: Sat Dec 31, 2016 11:18 am

split string into array/list based on delimiter?

Post by Omar1993 »

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!
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 560
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: split string into array/list based on delimiter?

Post by Sicro »

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
Image
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
User avatar
kenmo
Addict
Addict
Posts: 2043
Joined: Tue Dec 23, 2003 3:54 am

Re: split string into array/list based on delimiter?

Post by kenmo »

Sicro was faster.

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
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.
Omar1993
New User
New User
Posts: 7
Joined: Sat Dec 31, 2016 11:18 am

Re: split string into array/list based on delimiter?

Post by Omar1993 »

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!
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

Re: split string into array/list based on delimiter?

Post by citystate »

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
User avatar
jacdelad
Addict
Addict
Posts: 2010
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: split string into array/list based on delimiter?

Post by jacdelad »

@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
User avatar
mk-soft
Always Here
Always Here
Posts: 6246
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: split string into array/list based on delimiter?

Post by mk-soft »

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
Rinzwind
Enthusiast
Enthusiast
Posts: 690
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: split string into array/list based on delimiter?

Post by Rinzwind »

Should be optimized fast native command imho; split and join.
User avatar
skywalk
Addict
Addict
Posts: 4218
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: split string into array/list based on delimiter?

Post by skywalk »

FYI, regex is 1000x slower than optimized PB code. Use of StringField() is also slow.

You must choose when you need speed vs simplicity.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply