Autor Beitrag
stigge
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 426

WinXP
Delphi 2007
BeitragVerfasst: Fr 16.03.07 20:27 
Ja, ich mal wieder :wink:
Dieses Mal brauche ich einen guten Rat.
Ich will eine String-Liste nach einem String durchsuchen lassen und wenn er nicht existiert soll er hinzugefügt werden.
Welche Komponente benutze ich am Besten als String-Liste und wie kann ich sie dann durchsuchen lassen?
Jemand eine Idee???
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Fr 16.03.07 20:32 
Naja, für ne String-Liste würde ich mir ne Variable vom Typ TStringlist anlegen...

_________________
We are, we were and will not be.
JayEff
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2971

Windows Vista Ultimate
D7 Enterprise
BeitragVerfasst: Fr 16.03.07 20:32 
Als Stringliste empfehle ich dir, auf keinen Fall eine Komponente (Womöglich noch ein langsames Memo) zu nehmen, sondern eine Klasse, die Suche in: Delphi-Forum, Delphi-Library TSTRINGLIST
Die Suche findet sicher genug Beispiele, um alles zu erklären, aber die Delphi-OH schafft das auch ;)

_________________
>+++[>+++[>++++++++<-]<-]<++++[>++++[>>>+++++++<<<-]<-]<<++
[>++[>++[>>++++<<-]<-]<-]>>>>>++++++++++++++++++.+++++++.>++.-.<<.>>--.<+++++..<+.
stigge Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 426

WinXP
Delphi 2007
BeitragVerfasst: Fr 16.03.07 20:43 
Ich brauche halt eine Liste,

1) die man durchsuchen kann
2) wo man Items an einer bestimmten Stelle einfügen kann
z.B. vorher:
1) 1. String
2) 2. String
3) 3. String

Nachher:
1) 1. String
2) 2. String
3) Neu eingefügter String
4) 3. String
JayEff
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2971

Windows Vista Ultimate
D7 Enterprise
BeitragVerfasst: Fr 16.03.07 20:54 
Das musst du alles selbst implementieren, das kann man nicht einfach so. Du musst zum Einfügen z.B. mit einer Schleife durch die Liste gehen und alles eins nach oben schieben, was verschoben werden muss. Genauso beim Suchen: Mit einer Schleife durchgehen und per if-Bedingung vergleichen. Falls der Wert mit dem Suchwort übereinstimmt, einfach eine Variable auf die Zählvariable setzen, so weist du, wo die Übereinstimmung auftrat.

_________________
>+++[>+++[>++++++++<-]<-]<++++[>++++[>>>+++++++<<<-]<-]<<++
[>++[>++[>>++++<<-]<-]<-]>>>>>++++++++++++++++++.+++++++.>++.-.<<.>>--.<+++++..<+.
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Fr 16.03.07 20:58 
Die Klasse TStringlist kennt eine Methode Insert, mit der man in einer Liste an beliebiger Stelle einen String einfügen kann.

_________________
We are, we were and will not be.
stigge Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 426

WinXP
Delphi 2007
BeitragVerfasst: Fr 16.03.07 21:17 
Die letzten beiden Posts sind aber verdammt gegensätzlich^^
Und wie kann ich die Liste dann durchsuchen?
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Fr 16.03.07 21:25 
In einer Schleife. Die Liste fängt bei 0 an und geht bis myList.Count-1. Den String an Index i erhält man mit myList[i]. Und den ganzen Krempel findet man auch in der Delphi-Hilfe ;-).

_________________
We are, we were and will not be.
stigge Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 426

WinXP
Delphi 2007
BeitragVerfasst: Fr 16.03.07 21:27 
OK, jetzt habe ich aber trotzdem noch ein Problem: :twisted:
:oops: Wo finde ich TStringlist? :oops:
Blackheart666
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2195

XP
D3Prof, D6Pers.
BeitragVerfasst: Fr 16.03.07 21:30 
Die musst Du selbst erstellen, da gibts hier genug im Forum.
Und dann schau mal nach IndexOf das sollte Dir auch helfen.

_________________
Blackheart666
Der Irrsinn ist bei Einzelnen etwas Seltenes, - aber bei Gruppen, Parteien, Völkern, Zeiten die Regel. (Friedrich Nietzsche)
matze
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 4613
Erhaltene Danke: 24

XP home, prof
Delphi 2009 Prof,
BeitragVerfasst: Fr 16.03.07 21:33 
Zum Durchsuchen kennt die StringList aber auch Methoden. IndexOfzum Beispiel.

_________________
In the beginning was the word.
And the word was content-type: text/plain.
stigge Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 426

WinXP
Delphi 2007
BeitragVerfasst: Fr 16.03.07 21:41 
Aha...
ausblenden Delphi-Quelltext
1:
liste.IndexOf(edit2.text);					

Und wie kann ich jetzt herausfinden, ob die Suche erfolgreich war und wenn ja welchen itemindex das gefundene Element hat?
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Fr 16.03.07 21:48 
Hallo,

ist Deine Delphi-Hilfe kaputt? :wink:

Zitat:
Die Methode IndexOf gibt die Position eines Strings in der Liste zurück.

function IndexOf(const S: string): Integer; override;

Beschreibung

Mit IndexOf können Sie das erste Auftreten des Strings S ermitteln. IndexOf gibt den auf Null basierenden Index des Strings zurück. Wenn S also mit dem ersten String in der Liste übereinstimmt, liefert IndexOf 0 zurück, beim zweiten String 1 usw. Ist der String nicht vorhanden, gibt IndexOf -1 zurück.

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
stigge Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 426

WinXP
Delphi 2007
BeitragVerfasst: Fr 16.03.07 21:50 
user profile iconLannes hat folgendes geschrieben:
Hallo,

ist Deine Delphi-Hilfe kaputt? :wink:

Zitat:
Die Methode IndexOf gibt die Position eines Strings in der Liste zurück.

function IndexOf(const S: string): Integer; override;

Beschreibung

Mit IndexOf können Sie das erste Auftreten des Strings S ermitteln. IndexOf gibt den auf Null basierenden Index des Strings zurück. Wenn S also mit dem ersten String in der Liste übereinstimmt, liefert IndexOf 0 zurück, beim zweiten String 1 usw. Ist der String nicht vorhanden, gibt IndexOf -1 zurück.

Funktioniert schon aber ich blick öfter mal nicht durch :wink:
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Fr 16.03.07 21:52 
Hallo,

ausblenden Delphi-Quelltext
1:
2:
if liste.IndexOf(edit2.text) = -1 then
  //nicht enthalten;

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
stigge Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 426

WinXP
Delphi 2007
BeitragVerfasst: Fr 16.03.07 21:56 
user profile iconLannes hat folgendes geschrieben:
Hallo,

ausblenden Delphi-Quelltext
1:
2:
if liste.IndexOf(edit2.text) = -1 then
  //nicht enthalten;

Hab ich gerade selber herausgefunden^^
Blackheart666
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2195

XP
D3Prof, D6Pers.
BeitragVerfasst: Fr 16.03.07 21:59 
user profile iconstigge hat folgendes geschrieben:
user profile iconLannes hat folgendes geschrieben:
Hallo,

ausblenden Delphi-Quelltext
1:
2:
if liste.IndexOf(edit2.text) = -1 then
  //nicht enthalten;

Hab ich gerade selber herausgefunden^^

Das war doch auch jetzt nicht mehr allzu Schwer. :wink:

_________________
Blackheart666
Der Irrsinn ist bei Einzelnen etwas Seltenes, - aber bei Gruppen, Parteien, Völkern, Zeiten die Regel. (Friedrich Nietzsche)
stigge Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 426

WinXP
Delphi 2007
BeitragVerfasst: Sa 17.03.07 17:15 
Kann mir mal jemand sagen was daran falsch ist?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
var s: string;
    liste: tstringlist
    jetzt: integer;
    anzahl: integer;
.....
jetzt := 0;
anzahl := listbox2.Items.Count;
while jetzt < anzahl do
begin
s := listbox2.Items[jetzt];
FillID3TagInformation(s, edit1, edit2, edit3, edit4, edit5, edit6);
if liste.IndexOf(edit2.text) = -1 then
begin
liste.add('---> '+edit2.text);
end else
begin
indexnummer := liste.IndexOf(edit2.Text);
liste.Insert(indexnummer, '---> '+edit2.text);
end;
jetzt := jetzt + 1;
if jetzt = anzahl then
begin
liste.SaveToFile('liste.dat');
end;
end;
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Sa 17.03.07 17:30 
Und würdest du sagen, was genau passiert?
Erstellst du eine Instanz von TStringlist? Du gibst auch nichts frei.

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
stigge Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 426

WinXP
Delphi 2007
BeitragVerfasst: Sa 17.03.07 17:32 
user profile iconMarco D. hat folgendes geschrieben:
Und würdest du sagen, was genau passiert?
Erstellst du eine Instanz von TStringlist? Du gibst auch nichts frei.

Verstehe nicht genau was du meinst. Ich habe nur das gemacht, was ich im Post 1 weiter oben auch geschrieben habe.