Re: delimit string in a textfile

From: J French (erewhon_at_nowhere.uk)
Date: 10/31/04

  • Next message: Ante Smolcic: "Re: delimit string in a textfile"
    Date: Sun, 31 Oct 2004 14:13:48 +0000 (UTC)
    
    

    On 31 Oct 2004 03:29:29 -0800, harald_thomsen@yahoo.com (ht) wrote:

    <snip>

    >Every line starts with a letter.
    >In the first half I can use
    > date := copy(S, 20, 8)
    >as the colons are in fixed places.
    >
    >Can I find the colons using Pos, and then use Copy to get it?
    >
    >Not sure how it's done.

    You need to start with an InStr()

    http://tinyurl.com/5bsak

    PosEx seems to be in a later version of Delphi

    This also might be helpful

    unit Unit1;

    // Copy of code in USLib.pas

    interface

    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
    Dialogs,
      StdCtrls;

    type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;

    var
      Form1: TForm1;

    implementation

    {$R *.DFM}

    Type TDynStrArray = Array Of String;

    { ###########################################################
      17/11/03 JF
    }
    Function StrSplit( Const Src:String; Delin:String):TDynStrArray;
    Var
      Max, OldStart, Start :Integer;
      DelinPos :PChar;
      DelinLen :Integer;
    Begin
      Max := 0;

      // Bad Parameter
      If Delin = '' Then
         Begin
           SetLength( Result, 0 );
           MessageBox( 0, 'Bad Delineator in StrSplit()', 'Error', 0 );
           Exit;
         End;

      // Null String - Permitted
      If Src = '' Then
         Begin
           SetLength( Result, 0 );
           Exit;
         End;

      // Setup
      SetLength( Result, Length(Src)+1 );
      DelinLen := Length( Delin );
      DelinPos := Addr( Delin[1] );

      OldStart := 1;
      Start := 1;
      While Start <= ( Length(Src) - DelinLen + 1 ) Do
        Case CompareMem( Addr(Src[Start]), DelinPos, DelinLen ) Of
          True : Begin
                    Result[Max] := Copy( Src, OldStart, Start - OldStart
    );
                    Max := Max + 1;
                    Start := Start + DelinLen;
                    OldStart := Start;
                  End;
           False: Start := Start + 1
         End;

      // Take Residue ALWAYS
      // as if Delin is at End then another '' field is implied
      Result[Max] := Copy( Src, OldStart, MAXINT );
      Max := Max + 1;

      SetLength( Result, Max );
    End;

    procedure TForm1.Button1Click(Sender: TObject);
    Var
      AR :TDynStrArray;
      L9 :Integer;
      S :String;
    begin
      AR := StrSplit( '1,,22,,333,,4', ',,' );
      S := '';
      For L9 := Low( AR ) To High( AR ) Do
          S := S + AR[L9] + #13;
      ShowMessage( S + '----' );
    end;

    end.


  • Next message: Ante Smolcic: "Re: delimit string in a textfile"
  • Quantcast