Mega Code Archive

 
Categories / Delphi / Functions
 

GetToken function

Title: GetToken function Question: How to get a part of a string seperated by a special character? (e.g.: you have "AA,BB,CC,DD" and you want the second token which is here "BB") Answer: Syntax: ------- Gettoken('yourstring','seperation character',number of token); Example: -------- You have the string a:="AA,BB,CC,DD" and you want to get the second token which is here "BB". Try theresult:=gettoken(a,',',2); Function: --------- function GetToken(a: String; Sep: Char; Num: Byte):String; var Token:String; StrLen:Byte; TNum:Byte; TEnd:Byte; begin strLen:=Length(a); TNum:=1; TEnd:=StrLen; while ((TNum=Num) and (TEnd 0)) do begin TEnd:=Pos(Sep,a); if TEnd0 then begin Token:=Copy(a,1,TEnd-1); Delete(a,1,TEnd); Inc(TNum); end else Token:=a; end; if TNum=Num then Result:=Token else Result:=''; end;