jargon

improved "token" function from Sparked x86

Sep 12th, 2020 (edited)
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'my variation on tokens in FreeBASIC allows dynamic length delimiters, but only one per call, passed via the "sep" parameter.
  2. '"index' is a string parameter to the token function that either is "ct" to return the amount of enumerated tokens, or otherwise validates as a numeric in order to return the token by index.
  3.  
  4. function token(subject as string,sep as string=",",index as string="1") as string
  5.     dim as string buffer,Ln
  6.     dim as long o,t,i
  7.    
  8.     buffer=subject:t=0:i=val(index)
  9.    
  10.     if val(index)>0 then
  11.    
  12.         do
  13.             t=t+1
  14.             o=instr(1,buffer,sep):Ln=mid(buffer,o+len(sep)):buffer=mid(buffer,o+len(sep))
  15.            
  16.         loop until t=val(index)
  17.        
  18.         token=Ln
  19.        
  20.     elseif index="ct" then
  21.        
  22.         do
  23.             t=t+1
  24.             o=instr(1,buffer,sep):Ln=mid(buffer,o+len(sep)):buffer=mid(buffer,o+len(sep))
  25.        
  26.         loop until o=0
  27.        
  28.         token=str(t)
  29.    
  30.     end if
  31.    
  32. end function
Add Comment
Please, Sign In to add comment