Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Bestimmtes Wort in einem String zählen (https://www.delphipraxis.net/5202-bestimmtes-wort-einem-string-zaehlen.html)

Taralos 27. Mai 2003 18:33


Bestimmtes Wort in einem String zählen
 
Hallo,
ist es möglich eine bestimmt Wortgruppe in einem String zu zählen?

Also z.b. Wie oft kommt a im folgenden Wort vor?
Dann gebe ich Karawane ein und als Rückgabewert erhalte ich 3, weil das a 3mal vorkommt.

Ist sowas möglich?

Stanlay Hanks 27. Mai 2003 19:20

Delphi-Quellcode:
var i,a : Integer;
begin
For i:= 0 to Length(Edit1.Text) -1 do begin
 IF Edit1.Text[i] = 'a' //Sucht nach 'a'
 THEN a := a+1;
end;
ShowMessage('In diesem Text kommt der Buchstabe a '+
            IntToStr(a)+' mal vor');
end;

Taralos 27. Mai 2003 19:38

Vielen Dank für deine Hilfe!!
Das hat wunderbar geklappt :)

RomanK 27. Mai 2003 19:59

Hoi Stanlay,
so wird der Text aber nicht ganz durchsucht, ein a am Ende wird unterschlagen.
Edit1.Text[1] ist die erste Stelle und Edit1.Text[Length(Edit1.Text)] die letzte:
Delphi-Quellcode:
var i,a : Integer;
begin
For i:= 1 to Length(Edit1.Text) do begin
IF Edit1.Text[i] = 'a' //Sucht nach 'a'
THEN a := a+1;
end;
ShowMessage('In diesem Text kommt der Buchstabe a '+
            IntToStr(a)+' mal vor');
end;
So tut es richtig!

Stanlay Hanks 27. Mai 2003 20:34

Hallo FR. Ich unterwerfe mich in Demut. Das Problem hab ich schon immer. Ich mach das irgendwie immer falsch. Ich hab da immer so im Gedächtnis, dass der Textindex nullbasiert ist. Danke für die Korrektur!
man liest sich, Stanlay


Alle Zeitangaben in WEZ +1. Es ist jetzt 09:27 Uhr.

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