Page 1 of 1
split string into array/list based on delimiter?
Posted: Mon Apr 17, 2017 7:23 pm
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!
Re: split string into array/list based on delimiter?
Posted: Mon Apr 17, 2017 8:02 pm
by Sicro
https://github.com/SicroAtGIT/PureBasic ... oArray.pbiCode: 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
Re: split string into array/list based on delimiter?
Posted: Mon Apr 17, 2017 8:04 pm
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.
Re: split string into array/list based on delimiter?
Posted: Mon Apr 17, 2017 8:19 pm
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!
Re: split string into array/list based on delimiter?
Posted: Wed Apr 19, 2017 4:31 am
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
Re: split string into array/list based on delimiter?
Posted: Thu Sep 07, 2023 3:06 am
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.
Re: split string into array/list based on delimiter?
Posted: Thu Sep 07, 2023 7:41 am
by AZJIO
Re: split string into array/list based on delimiter?
Posted: Thu Sep 07, 2023 9:26 am
by mk-soft
Re: split string into array/list based on delimiter?
Posted: Fri Sep 08, 2023 5:59 am
by Rinzwind
Should be optimized fast native command imho; split and join.
Re: split string into array/list based on delimiter?
Posted: Fri Sep 08, 2023 3:07 pm
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.