Autor Beitrag
Muck22
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 23



BeitragVerfasst: Fr 26.05.06 16:51 
Warum gibt mir der folgende Code immer -1 zurück, sollte er nicht den IndexOf 'An' zurückgenen?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure TForm5.Button1Click(Sender: TObject);
  var s1:TStrings;
  var i1:Integer;


begin
    s1:=TStringList.Create;
    s1.LoadFromFile('C:\daten.txt');
     i1:=s1.IndexOf('An');
     showmessage(IntToStr(i1));
       s1.Free;
     end;
0xCC
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 150



BeitragVerfasst: Fr 26.05.06 16:55 
indexof liefert -1 zurück, wenn der text nicht gefunden wurde, ansonsten die nummer des items, das den text beinhaltet.
siehe delphi-hilfe (OH) und beispiel
Muck22 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 23



BeitragVerfasst: Fr 26.05.06 17:19 
Das wäre auch ok, aber das erste Wort in meiner StringList ist Andreas also müsste 'An' doch gefunden werden!
0xCC
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 150



BeitragVerfasst: Fr 26.05.06 17:23 
nein, es werden nur wörter gefunden , die genau gleich sind.
für deinen anwendungsfall musst du list.strings[0] - list.strings[list.count - 1] durchiterieren und mit pos nach deinem text suchen.
Muck22 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 23



BeitragVerfasst: Fr 26.05.06 17:26 
Danke, dann ist -1 natürlich richtig! :-)