LoginLogin

String Split Function

Root / Submissions / [.]

12Me21Created:
Usage: list$ = SPLIT$( string$, delimiter$ )
DEF SPLIT$(S$,D$)
 VAR LD%=LEN(D$)
 DIM R$[1]
 VAR N%=INSTR(S$,N%)
 IF N%==-1 THEN
  R$[0]=S$
  RETURN R$
 ENDIF
 R$[0]=LEFT$(S$,N%)
 @L
  VAR L%=N%+LD%
  N%=INSTR(L%,S$,D$)
 IF N%!=-1 THEN
  PUSH R$,MID$(S$,L%,N%-L%)
  GOTO @L
 ENDIF
 PUSH R$,MID$(S$,L%,&H7FFFFFFF)
 RETURN R$
END
Ex:
DIM A$[0]
A$ = SPLIT$("A**B**C","**")
?A$[1] 'B
(this could be much shorter but I wrote it a long time ago to be as fast as possible. If you want something faster to type but slightly less efficient you can use:
DEF SPLIT$(S,D)
 S=S+D
 DIM L$[0]
 @L
  VAR J,I=INSTR(J,S,D)
 IF I>=0 THEN PUSH L$,MID$(S,J,I-J):J=I+1:GOTO@L
 RETURN L$
END
(this is also not completely reliable with delimiters longer than 1 character)

This is anti-YARN propaganda anyway twelve you could probably consolidate an entire "useful functions" resource page

Replying to:snail_
This is anti-YARN propaganda anyway twelve you could probably consolidate an entire "useful functions" resource page
Well the reason I made this was specifically so it would be searchable, since I've posted string split functions like 10 times in replies and chat

12Me21's String Library

Replying to:snail_
This is anti-YARN propaganda anyway twelve you could probably consolidate an entire "useful functions" resource page
also wait, YARN is an acronym now?

Replying to:snail_
This is anti-YARN propaganda anyway twelve you could probably consolidate an entire "useful functions" resource page
Y'all Alright? Roy Noticed!