function like STRTOK()?

Blitz3D Forums/Blitz3D Programming/function like STRTOK()?

vivaigiochi(Posted 2011) [#1]
i seek a similar function, but exactly i seek for the better function similar to strtok.


D4NM4N(Posted 2011) [#2]
This function returns a numbered section (which) of a string (in), separated by the sep parameter.

This does not do exactly what you want but you could easily modify it to return a string array (or a string) with each string terminated by a "+chr(255)" for "\0" or whtever the char code is for "\n" or "\n\r" depending on how you want them terminated... or not if you just want the strings.

Function USV$(in$,which%=1,sep$=",")
;''pipe seprated values
	Local n% = 1
	Local offset% = 0
	Local nextoffset% = 1
	Local ValueRet$ =""
	
	While offset<Len(in$)
		nextoffset = Instr(in$,sep$,offset+1)
		If nextoffset = 0
			nextoffset = Len(in$)+1
			which = n
		End If
		valueret$ = Mid$(in$,offset+1,nextoffset-offset-1)
		If which = n	
			Return valueret	
		End If
		offset = nextoffset
		n=n+1
	Wend

	Return n-1

End Function




Rroff(Posted 2011) [#3]
Function subStr$(iStr$,pos,token$=" ",quotes=1,all=0)
	Local sStart
	Local sSpace
	Local fQuote
	Local eQuote
	Local sub
	Local sSub$
	Local lStart
	Local lLen
	Local lQuote
	
	If (iStr$ = "") Then
		Return ""
	EndIf
	
	If (pos < 1) Then
		Return
	EndIf
	
	While (sub < pos)
		sub = sub + 1
		If (sStart+1 > Len(iStr$)) Then
			Return ""
		EndIf

		sSpace = Instr(iStr$,token$,sStart+1)
		fQuote = Instr(iStr$,Chr$(34),sStart+1)
		
		If ((Not sSpace) And (Not fQuote)) Then
			If (sub < pos) Then
				Return ""
			Else
				Return Mid$(iStr$,sStart+1,Len(iStr$)-sStart)
			EndIf
		EndIf
		
		If ((fQuote > 0) And (fQuote < sSpace) And (quotes=1)) Then
			eQuote = Instr(iStr$,Chr$(34),fQuote+1)

			If (Not eQuote) Then
				Return ""
			EndIf
			
			sSub$ = Mid$(iStr$,fQuote+1,(eQuote-fQuote)-1)
			
			lStart = fQuote+1
			lQuote = 1
			
			sStart = eQuote+1
		ElseIf ((fQuote > 0) And (Not sSpace) And (quotes=1)) Then
			eQuote = Instr(iStr$,Chr$(34),fQuote+1)

			If (Not eQuote) Then
				Return ""
			EndIf
			
			sSub$ = Mid$(iStr$,fQuote+1,(eQuote-fQuote)-1)
			
			lStart = fQuote+1
			lQuote = 1
			
			sStart = eQuote+1		
		Else
			sSub$ = Mid$(iStr$,sStart+1,(sSpace-sStart)-1)
			
			lStart = sStart
			lQuote = 0
			
			sStart = sSpace
		EndIf
	Wend
	
	If (all = 1) Then
		If (lQuote = 1) Then
			lLen = Len(iStr$)
			If (lLen-lStart > 0) Then
				Return Mid$(iStr$,lStart,lLen-lStart)
			Else
				Return ""
			EndIf
		Else
			lLen = Len(iStr$)
			If (lLen-lStart > 0) Then
				Return Right$(iStr$,lLen-lStart)
			Else
				Return ""
			EndIf
		EndIf
	EndIf
	
	Return sSub$

End Function


Similiar function that I use - think it all works properly but long time since I played with it.