Title: Token parsing
Question: If you are having trouble parsing delimited strings, these three functions are perfect for you.
Answer:
// I'm sure that you will find these three string handling and token parsing functions helpful!
function GetToken(TokenData: String; ItemIndex: Integer; TokenDelimiter: String): String;
var
i, Len, DelimLen, TokenCount: Integer;
TokenString: String;
begin
// skoot thru the Token Data and parse out the specified Item using TokenDelimiter
TokenCount := 0;
i := 0;
TokenString := '';
Len := Length(TokenData);
DelimLen := Length(TokenDelimiter);
if (Len 0) then begin
while i if (TokenCount = (ItemIndex - 1)) then begin
if (Copy(TokenData,i+1,DelimLen) = TokenDelimiter) then
break;
TokenString := TokenString + Copy(TokenData,i+1,1);
end;
if (Copy(TokenData,i+1,DelimLen) = TokenDelimiter) then
begin
Inc(TokenCount);
Inc(i, DelimLen-1);
end;
Inc(i);
end;
end;
Result := TokenString;
end;
function SetToken(TokenData: String; ItemIndex: Integer; NewToken: String; TokenDelimiter: String): String;
var
i, Len, TokenCount: Integer;
FrontPiece, BackPiece, TokenString: String;
begin
TokenCount := 0;
Len := Length(TokenData);
if (Len 0) then begin
// first, count the tokens and make sure that we have enough, if not, then
// add tokens to the end so that we don't drive out into hyperspace
for i := 0 to Len do begin
if (AnsiString(TokenData[i+1]) = TokenDelimiter) then
Inc(TokenCount);
end;
end;
if (TokenCount TokenData := TokenData + Replicate(TokenDelimiter,(ItemIndex-TokenCount));
end;
TokenString := '';
FrontPiece := Char(0);
BackPiece := '';
TokenCount := 0;
Len := Length(TokenData);
for i := 0 to Len do begin
if (TokenCount = (ItemIndex - 1)) then begin
if (FrontPiece = Char(0)) then
FrontPiece := Copy(TokenData,1,i);
if (Copy(TokenData,i+1,1) = TokenDelimiter) then
break;
end;
if (AnsiString(TokenData[i+1]) = TokenDelimiter) then
Inc(TokenCount);
end;
BackPiece := Copy(TokenData,i+1,Len-i+1);
TokenString := FrontPiece + NewToken + BackPiece;
Result := TokenString;
end;
function GetTokenCount(TokenData: String; TokenDelimiter: String): Integer;
var
i, Len, DelimLen, TokenCount: Integer;
begin
// skoot thru the Token Data and parse out the specified Item using TokenDelomiter
TokenCount := 0;
i := 0;
Len := Length(TokenData);
DelimLen := Length(TokenDelimiter);
if (Len 0) then begin
while i if (Copy(TokenData,i+1,DelimLen) = TokenDelimiter) then
begin
Inc(TokenCount);
Inc(i,DelimLen-1);
end;
Inc(i);
end;
end;
Result := TokenCount;
end;