Results 1 to 9 of 9

Thread: strtok in delphi

  1. #1

    strtok in delphi

    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
    Imagine I've written something clever here inspiring you to make something awesome. If that happens give me credits

  2. #2

    strtok in delphi

    What does it do? It might be easy enough to make.

  3. #3

    strtok in delphi

    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.
    Imagine I've written something clever here inspiring you to make something awesome. If that happens give me credits

  4. #4

    strtok in delphi

    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.

  5. #5

    strtok in delphi

    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?
    Imagine I've written something clever here inspiring you to make something awesome. If that happens give me credits

  6. #6

    strtok in delphi

    Quote Originally Posted by pstudio
    I use Turbo Delphi
    I 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.


    Quote Originally Posted by pstudio
    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:
    Code:
    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.

  7. #7

    strtok in delphi

    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.
    <A HREF="http://www.myhpf.co.uk/banner.asp?friend=139328">
    <br /><IMG SRC="http://www.myhpf.co.uk/banners/60x468.gif" BORDER="0">
    <br /></A>

  8. #8

    strtok in delphi

    @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.
    Imagine I've written something clever here inspiring you to make something awesome. If that happens give me credits

  9. #9

    strtok in delphi

    Hi have one like that it in my utils libs;


    [pascal]
    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;





    [/pascal]







    "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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •