How to split a string

BlitzPlus Forums/BlitzPlus Beginners Area/How to split a string

laughing_man(Posted 2014) [#1]
How to split a string? The string is a record and contains ascii 31 as a field delimiter. Each record is containing a name and a number; both strings of non fixed length. I may add another field. So lets consider 3 fields instead of just 2.


Yasha(Posted 2014) [#2]
There are several entries for this in the code archives - here's one: http://www.blitzbasic.com/codearcs/codearcs.php?code=2991

(this is one of those things people love to keep remaking and re-adding - search the "Miscellaneous" and "Algorithm" sections for "split" and you'll find lots more examples)


Matty(Posted 2014) [#3]
Be careful with some of those code archives. ...some of them dont always behave as expected with consecutive delimiters...ie empty fields.


laughing_man(Posted 2014) [#4]
OK Thanks!


RemiD(Posted 2014) [#5]
How i would do it, in words :

count the number of characters in the instring (the "length" of the string)
each character has a position in the string from 1 to count (this can be from 0 to count-1 in some others langages)
create an array to store each outstring
the current outstring id is 1
for each pos (from 1 to count)
read the character at this pos in the instring
if the character does not correspond to the character you use for a delimiter
add this char to the current outstring
elseif the character corresponds to the character you use for a delimiter
create another outstring
the current outstring id is id+1
(do not add this char to the current outstring)

useful functions :
Len()
Mid()


laughing_man(Posted 2014) [#6]
Thank You.