Delphi-PRAXiS
Seite 1 von 4  1 23     Letzte »    

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi String in Array zerlegen (Markanter Punkt: ',') (https://www.delphipraxis.net/1203-string-array-zerlegen-markanter-punkt.html)

rebugger 31. Okt 2002 16:59


String in Array zerlegen (Markanter Punkt: ',')
 
Hallo,
habe einen String, der folgendermaßen aussieht:
var1:='$0045CDFA,clNone,0,1,0,0'

Nun möchte ich var1 in ein Array zerlegen, dass in $var2[0] '$0045CDFA'
und in $var2[1] 'clNone', usw. steht !

Wie kann ich das machen ?
Wie lautet der Befehl ?

jbg 31. Okt 2002 17:04

Re: String in Array zerlegen (Markanter Punkt: ',')
 
Zitat:

Zitat von rebugger
Wie kann ich das machen ?

Zumindest nicht so wie in PHP.

Zitat:

Wie lautet der Befehl ?
Ein von Delphi bereitgestellter Befehl ist mir nicht bekannt. Da heißt es wohl selbst Hand anlegen.
-> Pos(), Copy(), ggf. auch Delete()

Luckie 31. Okt 2002 17:07

Code:
[b]type[/b] TZeile = [b]array[/b] [b]of[/b] [b]string[/b];

[color=#000080][i]{ Zeile parsen }[/i][/color]
[b]function[/b] Parse(zeile: [b]string[/b];zeilennummer:longint ):TZeile;
[b]var[/b] i    :longint;
    anzahl:longint;
    pos  :[b]array[/b] [b]of[/b] integer;
[b]begin[/b]
  anzahl:=0;
  SetLength(pos,0);
  SetLength(result, 0);
  [b]for[/b] i := 1 [b]to[/b] Length(zeile) [b]do[/b]
    [b]if[/b] zeile[ i ] = ';' [b]then[/b] // <-- ACHTUNG KORRIGIERT!!!
    [b]begin[/b]
      inc(anzahl);
      SetLength(pos, anzahl);
      pos[anzahl-1]:=i;
      SetLength(result, anzahl);
    [b]end[/b];
  [b]for[/b] i:=0 [b]to[/b] high(result) [b]do[/b]
    [b]begin[/b]
      [b]if[/b] i=0 [b]then[/b]
        result[i ]:=copy(zeile,1,pos[0]-1)
      [b]else[/b]
        result[i ]:=copy(zeile,pos[i-1]+1,pos[i ]-1-pos[i-1]); // <-- ACHTUNG KORRIGIERT!!!
    [b]end[/b];
  [color=#000080][i ]//if Length(result)=0 then Messagebox(0, '', 'Fehler', 0);[/i][/color]
[b]end[/b];
Und auf ruf mit:
Code:
[b]var[/b]
  Zeile : TZeile;
[b]begin[/b]
  Zeile := Parse(s, 0);
Auf die einzelnen Elemente kannst du dann mit den Index zugreifen. Das erste Element bekommst du dann mit Zeile[0].

rebugger 31. Okt 2002 17:24

Habe es nun so gemacht:
Code:
type TZeile = array of string;
.
.
.
//Deine Funktion
.
.
.
function TMain.Intialize_Highlight(): Bool;
var
  ini: TIniFile;
  php_comment, php_identifier, php_invalidsymbol, php_key,
  php_number, php_space, php_string, php_symbol, php_variable: TZeile;
  tmp_style: TFontStyle;
begin
  ini := TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'highlight.cfg');
  try
    php_comment := Parse(ini.ReadString('PHP','Comment','$0045CDFA,clNone,0,1,0,0'), 0);
    { Zeile 180 }PHPhigh.CommentAttri.Foreground := IntToStr(php_comment[0]);
    PHPhigh.CommentAttri.Background := IntToStr(php_comment[1]);

    if IntToStr(php_comment[2]) = 1 then
      PHPhigh.CommentAttri.Style := fsBold;
    if IntToStr(php_comment[3]) = 1 then
      PHPhigh.CommentAttri.Style := fsItalic;
    if IntToStr(php_comment[4]) = 1 then
      PHPhigh.CommentAttri.Style := fsUnderline;
    if IntToStr(php_comment[5]) = 1 then
      PHPhigh.CommentAttri.Style := fsStrikeOut;

  finally
    ini.Free;
  end;
end;
Aber er sagt:
[Fehler] main_unit.pas(180): Es gibt keine überladene Version von 'IntToStr', die man mit diesen Argumenten aufrufen kann

Luckie 31. Okt 2002 17:35

php_comment[2]

Das ist doch schon ein String. Kuck dir doch mal die Typdeklarartion von TZeile an: array of String.

MathiasSimmack 1. Nov 2002 07:01

Re: String in Array zerlegen (Markanter Punkt: ',')
 
Zitat:

Zitat von jbg
Ein von Delphi bereitgestellter Befehl ist mir nicht bekannt.

Mir auch nicht, aber wenn es immer bei einem Komma bleiben wird, dann hätte ich die Eigenschaft "CommaText" von TStringList herangezogen.

Zitat:

Da heißt es wohl selbst Hand anlegen.
Ja. Und die Idee mit dem eigenen Parser ist auf jeden Fall eleganter, wenn man davon ausgeht, dass sich der Separator ändern könnte.

Luckie 1. Nov 2002 12:18

ACHTUNG, ACHTUNG der Coderformatierer hat ein paar [*i*]'s geschluckt. Ich habe es korrigiert. Bitte überprüf noch mal deinen Code. Danke

Christian Seehase 1. Nov 2002 13:28

Moin Luckie,

ich hab' jetzt mal den Code aus dem Posting genommen, alle Blanks vor bzw. hinter den eckigen Klammern entfernt und den Formatierer laufen lassen.

Kein Fehler feststellbar. :shock:

rebugger 1. Nov 2002 13:33

OK, Alles nochmal:
Code:
type TZeile = array of string;
.
  public
    { Public-Deklarationen }
    function Intialize_Highlight(): Bool;
    function Parse(zeile: string;zeilennummer:longint ):TZeile;
.
function TMain.Parse(zeile: string;zeilennummer:longint ):TZeile;
var i    :longint;
    anzahl:longint;
    pos  :array of integer;
begin
  anzahl:=0;
  SetLength(pos,0);
  SetLength(result, 0);
  for i := 1 to Length(zeile) do
    if zeile[ i ] = ';' then // <-- ACHTUNG KORRIGIERT!!!
    begin
      inc(anzahl);
      SetLength(pos, anzahl);
      pos[anzahl-1]:=i;
      SetLength(result, anzahl);
    end;
  for i:=0 to high(result) do
    begin
      if i=0 then
        result[i ]:=copy(zeile,1,pos[0]-1)
      else
        result[i ]:=copy(zeile,pos[i-1]+1,pos-1-pos[i-1]); // <-- ACHTUNG KORRIGIERT!!!
    end;
  //if Length(result)=0 then Messagebox(0, '', 'Fehler', 0);
end;

function TMain.Intialize_Highlight(): Bool;
var
  ini: TIniFile;
  php_comment, php_identifier, php_invalidsymbol, php_key,
  php_number, php_space, php_string, php_symbol, php_variable: TZeile;
  tmp_style: TZeile;
begin
  ini := TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'highlight.cfg');
  try
    php_comment := Parse(ini.ReadString('PHP','Comment','$0045CDFA,clNone,0,1,0,0'), 0);
    PHPhigh.CommentAttri.Foreground := IntToStr(php_comment[0]);
    PHPhigh.CommentAttri.Background := IntToStr(php_comment[1]);

    if IntToStr(php_comment[2]) = 1 then
      PHPhigh.CommentAttri.Style := fsBold;
    if IntToStr(php_comment[3]) = 1 then
      PHPhigh.CommentAttri.Style := fsItalic;
    if IntToStr(php_comment[4]) = 1 then
      PHPhigh.CommentAttri.Style := fsUnderline;
    if IntToStr(php_comment[5]) = 1 then
      PHPhigh.CommentAttri.Style := fsStrikeOut;

  finally
    ini.Free;
  end;
end;
Er sagt mir:
[Fehler] main_unit.pas(157): Operator ist auf diesen Operandentyp nicht anwendbar
Die Zeile:
Code:
result[i ]:=copy(zeile,pos[i-1]+1,pos-1-pos[i-1]);
Wo liegt der Fehler ?
Kann ich das ganze nicht verkürzen ?
Ich müsste das ganze sonst noch mit PHPhigh.IdentifierAttri.... und noch weiteren 40 Objekten machen und das wäre sehr viel Code !
Kann man das alles nicht in eine Funktion zusammenfassen, an die ich nur noch z.B. "PHPHigh.CommentAttri" übergeben muss ?

Luckie 1. Nov 2002 13:36

Also bei mir tut es die Funktion jedes mal. Keinen Plan, was du da gemacht hast. :?


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:14 Uhr.
Seite 1 von 4  1 23     Letzte »    

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz