this slowpoke moves

Split Strings

type
  TStrArray=array of string;
  
private
    { Declarations privates }
    procedure Split(const Delimiter: char; DelimitedText: string; var StrArray: TStrArray);
    
var
  Form1: TForm1;
  Delimiter: char;
  
const
  ARRAY_LEN='Arrays length is equal to ';

//

procedure TForm1.Edit1Change(Sender: TObject);
begin
  ListBox1.Items.Clear;
  with Label2 do begin
    Caption:=ARRAY_LEN;
    Visible:=False;
  end;
end;

procedure TForm1.Split(const Delimiter: char; DelimitedText: string; var StrArray: TStrArray);
var
  i, j, l: cardinal;
  k: byte;
begin
  if (DelimitedText=EmptyStr) then begin
    SetLength(StrArray, 1);
    StrArray[0]:=EmptyStr;
  end else if (Delimiter=#0) then begin
    SetLength(StrArray, 1);
    StrArray[0]:=DelimitedText;
  end else begin
    l:=Length(DelimitedText);
    if (Copy(DelimitedText, l, 1)<>Delimiter) then begin
      DelimitedText:=DelimitedText + Delimiter;
      l:=l+1;
    end;
    SetLength(StrArray, 256);
    k:=0;j:=0;
    for i:=1 to l do
      if (DelimitedText[i]=Delimiter) then begin
        inc(k);
        strArray[k-1]:=Copy(DelimitedText, j+1, i-(j+1));
        j:=i;
      end;
    SetLength(StrArray, k);
  end;
end;

procedure TForm1.FormActivate(Sender: TObject);
procedure setCursor(hWnd:integer; lpCursorName:PAnsiChar);
  var
    hCur:integer;
  begin
    hCur:=LoadCursor(0, lpCursorName);
    if (hCur<>0) then begin
      SetClassLong(hWnd, GCL_HCURSOR, hCur);
      DestroyCursor(hCur);
    end;
  end;
begin

  Delimiter:=';';       // HIER ANGEBEN WANN GETRENNT WERDEN SOLL

  Label1.Caption:= 'Delimiter: "' + Delimiter + '"';
  setCursor(Button1.Handle, IDC_HAND);
  setCursor(Button1.Handle, IDC_HAND);
end;
Beispiel :
procedure TForm1.Button1Click(Sender: TObject);
var
  StrArray: TStrArray;
  i: byte;
begin
  Split(Delimiter, Edit1.Text, StrArray);
  for i:=Low(StrArray) to High(StrArray) do
    ListBox1.Items.Add(StrArray[i]);
  with Label2 do begin
    Visible:=True;
    Caption:=ARRAY_LEN + IntToStr(ListBox1.Items.Count);
  end;
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate