Autor Beitrag
Grolle
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 95

Win XP, Win 7
Delphi 2010 Pro
BeitragVerfasst: Di 23.05.06 10:23 
Hi,

ich will nach dem 7 Komma in einer Memo Zeile ein neues Komma einfügen. Klappt
natürlich nicht und ich finde den Fehler nicht:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
var
i,j: integer;
s : string;
begin
  if (Memo1.Lines.Text <> '') and (tf = true) then
    begin
      for i := 0 to Memo1.Lines.Count-1 do
        begin
          s := Memo1.Lines[i];
          j := PosEx(',',s,6);
          Insert(',',s,j+1);
          Memo1.Lines[i] := s;
        end;
    end;
end;


Viele Grüße...


Zuletzt bearbeitet von Grolle am Di 23.05.06 10:50, insgesamt 1-mal bearbeitet
Arno Nym
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 131



BeitragVerfasst: Di 23.05.06 10:38 
Hi,
Also die Zeile
ausblenden Quelltext
1:
j := PosEx(',',s,);					

hat ja schonmal Syntaxfehler
warum nicht einfach
ausblenden Quelltext
1:
j := Pos(',',s);					

ist das mit der 7 in "ich will nach dem 7 Komma in einer Memo Zeile..." ein Vertipper, oder sollen da wirklich erst 7 Kommata stehen ?

MFG Arno, Nym
Grolle Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 95

Win XP, Win 7
Delphi 2010 Pro
BeitragVerfasst: Di 23.05.06 10:46 
Hi,

nein, da sollen wirklich erst 7 Kommas stehen (dazwischen natürlich Text). Ist eine CSV-Datei.
// Edit Wegen dem Syntaxfehler: Text oben editiert

Viele Grüße...
Arno Nym
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 131



BeitragVerfasst: Di 23.05.06 11:12 
Hi,
Also wenn es ein normaler CSV-String ist warum machst du es nich einfach so:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
 VAR SL : TStrings;
 .
 .
 .
 SL := TStringlist.Create;
 SL.CommaText := s; //dein CSV-String
 IF SL.Count = 7 THEN BEGIN;
  // evtl wieder als Text ausgeben lassen und Komma anfügen, oder was du auch immer machn willst...
 END;
 SL.Free;


Jedenfalls lässt sich das so wesentlich besser verarbeiten.

MFG, Arno Nym

Moderiert von user profile iconraziel: Code- durch Delphi-Tags ersetzt
Grolle Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 95

Win XP, Win 7
Delphi 2010 Pro
BeitragVerfasst: Di 23.05.06 11:24 
Hi,

und wie bekomme ich dann die Position des 7. Kommas in einer Zeile?
StringList.Count zählt doch nur die Zeilen, oder nicht?

Viele Grüße....
Arno Nym
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 131



BeitragVerfasst: Di 23.05.06 11:49 
Hi,
In der Stringlist sind doch dann gar keine Kommata mehr drin,
Also wenn dein String s = 'Hallo, ich, suche, das, siebte, Komma, Danke' ist;
dann liefert bei "IF SL.Count > 7 THEN BEGIN;" schonmal den Test ob überhaupt sieben Kommas drin waren.
das ergebnis SL[7...count] liefert den Text nach dem Komma Nr.7
und SL[0..5] den die Strings davor.

Wenn du aber unbedingt ohne TStrings arbeiten willst dann hab ich dir hier ne Funktion geschrieben die auch so geht:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
//sucht das n-te Vorkommen von Str1, in Str2.. (Str1 muss length=1 haben)
// Ergebnis ist -1 wenn der String nicht n-mal vorkommt
Function WhereIstheString(Str1,Str2:String; n:Integer):Integer;
var i,i2: Integer;
begin;
 i2 := 0;
 result := -1;
 For i := 1 To length(Str2) do begin;
  IF Str2[i] = Str1 then inc(i2);
  IF i2 = n Then begin;
   result := i;
   break;
  end;
 end;
end;


MFG, Arno Nym

Moderiert von user profile iconraziel: Code- durch Delphi-Tags ersetzt
Grolle Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 95

Win XP, Win 7
Delphi 2010 Pro
BeitragVerfasst: Di 23.05.06 12:09 
Alles klar. Das klappt!

Vielen Dank!