PDA

View Full Version : strtok in delphi



pstudio
14-11-2007, 08:13 PM
Compiler: Delphi
Os: win

Is there an equilavent to the c string library function strtok(char*, cons char*) in Delphi? I've searched for it, but couldn't find it :(

Robert Kosek
14-11-2007, 08:26 PM
What does it do? It might be easy enough to make.

pstudio
14-11-2007, 09:42 PM
I probably could make it or something else to suit my needs.
It would just be a waste of my time if it already exists.

What does it do?
It takes two strings. The first string is the string you want to split up in tokens. The second string identifies the delimeter. First time you call the function it returns the first token. If you then call the function again but with a nil in the first argument, you recieve the next token in the previous given string. When there are no more tokens left the functions return nil.

Robert Kosek
14-11-2007, 09:52 PM
I never was aware of this function's equivalent in Delphi, what version do you have? I think FPC has this, but it's easy enough to code to return an array of strings or a stack containing the tokens.

pstudio
14-11-2007, 10:11 PM
I never was aware of this function's equivalent in Delphi, what version do you have?
I use Turbo Delphi


it's easy enough to code to return an array of strings or a stack containing the tokens.
I know :)


I think FPC has this
Do you have any idea what it's called and where to locate it?

Robert Kosek
14-11-2007, 10:18 PM
I use Turbo DelphiI used it for a few months, and I don't recall stumbling into it. I haven't reinstalled presently; someone else will have to confirm it's presence/absence.




I think FPC has this
Do you have any idea what it's called and where to locate it?Yup. StrUtils has 3 useful functions for you:
function ExtractDelimited(N: Integer;const S: String; const Delims: TSysCharSet) : String;

function ExtractWord(N: Integer;const S: String; const WordDelims: TSysCharSet) : String;

function WordCount(const S: String;const WordDelims: TSysCharSet): Integer;Delphi may have equivalents if you search the documentation or the source files.

technomage
14-11-2007, 10:21 PM
A String list has the capability to split an input string up into items in the list.

For example you can initialize a string list DelimitedText property with

A=1;B=2;C=2

and it will produce a list with

string value
A 1
B 2
C 3

On later versions of delphi you can change the Delimiter value.

see http://www.delphibasics.co.uk/RTL.asp?Name=TStringList for an example.

pstudio
14-11-2007, 10:40 PM
@technomage
I'm aware of TStringList and it's abilities to split strings up. That's what I'm doing right now. :)

@Kosek
I'll take a look at those 3 functions.

tpascal
15-11-2007, 06:11 PM
Hi have one like that it in my utils libs;



function GetTokenCount(Cadena,Separador:string):integer;
var
Posicion:integer;
begin
Posicion:=Pos(Separador,Cadena);
Result:=1;

if Posicion <> 0 then
while Posicion <0> 1 do begin
Delete(Cadena,1,Pos(Separador,Cadena));
Dec(Token);
end;
Posicion:=Pos(Separador,Cadena);
if Posicion=0
then result:=cadena
else Result:=Copy(Cadena,1,Posicion-Length(Separador));
end;

function GetToken(Cadena,Separador:string;Token:integer):st ring;
var
Posicion:integer;
begin
while Token > 1 do begin
Delete(Cadena,1,Pos(Separador,Cadena));
Dec(Token);
end;
Posicion:=Pos(Separador,Cadena);
if Posicion=0
then result:=cadena
else Result:=Copy(Cadena,1,Posicion-Length(Separador));
end;













"cadena" mean the string where you want to search for tokens,
"separador" mean the string used as separator.

Note that separator can be any length.

example:

a:='za-bb-cx-dr-er-xz'

n:=gettokencount(a,'-'); //this will return integer value 6 which mean 6 token are available in the string.

s:=gettoken(a,'-',4); //will return string "dr", which is the 4th token in the string.



hope it helps