SplitString() to split a string by a delimiter into an array

Share your advanced PureBasic knowledge/code with the community.
technicorn
Enthusiast
Enthusiast
Posts: 105
Joined: Wed Jan 18, 2006 7:40 pm
Location: Hamburg

SplitString() to split a string by a delimiter into an array

Post by technicorn »

There was a request for splitting strings into parts in the wishlist section,
so I wrote this little procedure:

Code: Select all

; Splitting a string into parts
;
; *s = Pointer to string to split
; sa() = String-array which will contain the parts
; delimiter = Character by which the string will be splitted
;
; Returns the number of parts
;
; If the string is empty, sa() will not be changed
; and the return value is 0
Procedure.l SplitString(*s.Character, sa.s(1), delimiter.c)
  Protected saUBound.l = 15, splitCnt.l, *splitStart.c, ch.c
  
  If *s
    If *s\c
      ReDim sa.s(saUBound)
      Repeat
        *splitStart = *s
        ch = *s\c
        While ch And ch <> delimiter
          *s + SizeOf(Character)
          ch = *s\c
        Wend
        If splitCnt > saUBound
          saUBound + 16
          ReDim sa.s(saUBound)
        EndIf
        sa(splitCnt) = PeekS(*splitStart, *s - *splitStart)
        splitCnt + 1
        *s + SizeOf(Character)
      Until Not ch
      ReDim sa.s(splitCnt - 1)
    EndIf
  EndIf
  
  ProcedureReturn splitCnt
EndProcedure
It will be a little slower than StringField() for strings that only contains one or two fields,
but it is much faster for more parts and longer strings.

Happy coding
technicorn

Edit:
For more speed gain you can remove the "ReDim sa.s(splitCnt - 1)" at the end,
than this Proc. is twice as fast as StringField() even for strings with only one part!
The littel memory overhead for empty strings is worth it, I think.
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

I'm a little too lazy today to check so I'll just ask ;)

Can this split on a CRLF or is it only single characters that it can split on (like a comma, tab etc)
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
r_hyde
Enthusiast
Enthusiast
Posts: 155
Joined: Wed Jul 05, 2006 12:40 am

Post by r_hyde »

Looks like it only splits on a single char. It looks easy enough to modify it to split on multichar delimiters, though. Thanks for this and joinstring(), techincorn. Could come in handy!
technicorn
Enthusiast
Enthusiast
Posts: 105
Joined: Wed Jan 18, 2006 7:40 pm
Location: Hamburg

Post by technicorn »

r_hyde:
Looks like it only splits on a single char. It looks easy enough to modify it to split on multichar delimiters, though. Thanks for this and joinstring(), techincorn. Could come in handy!
You'r welcome and you'r right, it's only for single character delimiter,
I'll post a version for multi delimiter split/join today.
Post Reply