![]() |
Frage zu einer funktion
hoi,
ich hab ne frage und zwar hierzu :
Delphi-Quellcode:
function CountWords(str: string; sep: char): integer;
begin result := 0; if str <> '' then // Wenn str nicht leer ist begin if pos(sep, str) <> 0 then // Wenn chr in str ist begin while Copy(str, 1, 1) = sep do // Solange zeichen1 löschen bis es nichtmehr chr ist (wenn überhaupt?) Delete(str, 1, 1); while pos(sep, str) <> 0 do // hier muss ich schon wieder abfragen ob chr noch in str ist? (siehe if-abfrage2) begin inc(Result); Delete(str, 1, Pos(sep, str)); end; if str <> '' then // hier muss ich schon wieder abfragen ob str nicht leer ist? (siehe if-abfrage1) inc(Result); end else inc(Result); end else Result := 0; end; ich krieg es nicht anders hin, gibt es ne alternative? Der code funzt einwandfrei, ich übergebe einen string der in wörter geteilt wird anhand des seperators... aber ich finde das ist für sone kleinigkeit vielzuviel geschrieben oder nicht? z.b das ich 2 abfragen doppelt hab (aber wie gesagt anders bekomm ichs nicht hin). ich wollt mal wissen wie ihr das gemacht höättet, damit ich mir n paar (bessere?) beispiele angucken kann |
Re: Frage zu einer funktion
Hi!
Wie wärs damit? Könnte aber auch das Gleiche sein! Ist mir nur gerade eingefallen. Ist aber genau so lang!!! ![]() mfg cyberfreak |
Re: Frage zu einer funktion
so hab mal ne funktion von mir die eigentlich das ganze zerlegt und als stringlist zurückgibt abgeändert.
Delphi-Quellcode:
function countitems(Source: String; Delimiter: String):Integer;
var count: Integer; toadd : String; begin result := 0; count := 1; toadd := ''; while count <= length(Source) do begin if copy(Source, count, length(Delimiter)) = Delimiter then begin if toadd <> '' then result := result + 1; toadd := ''; count := count + length(Delimiter); end else begin toadd := toadd + Source[count]; count := count + 1; end; end; if toadd <> '' then result := result + 1; end; |
Re: Frage zu einer funktion
Hier meine Lösung:
Delphi-Quellcode:
Der Aufruf erfolgt wie folgend:
procedure CountWords(Txt: AnsiString; var Words, Spaces, SentenceChars,
OtherChars: Integer); var InLegalWord: Boolean; I: Integer; const SENTENCE_SET = '.,;!?'; begin Words := 0; Spaces := 0; SentenceChars := 0; InLegalWord := False; for I := 1 to Length(Txt) do begin if Txt[I] in ['A'..'Z', 'a'..'z'] then begin InLegalWord := True; end else begin if InLegalWord then begin Inc(Words); InLegalWord := False; end; if Txt[I] in [#0..#32] then Inc(Spaces) else if Pos(Txt[I], SENTENCE_SET) > 0 then Inc(SentenceChars) else Inc(OtherChars); end; end; end;
Delphi-Quellcode:
...:cat:...
procedure TForm1.Button1Click(Sender: TObject);
var Words, Spaces, SentenceChars, OtherChars: Integer; begin CountWords(Memo1.Text, Words, Spaces, SentenceChars, OtherChars); Label1.Caption := IntToStr(Words); Label2.Caption := IntToStr(Spaces); Label3.Caption := IntToStr(SentenceChars); Label4.Caption := IntToStr(OtherChars); end; |
Re: Frage zu einer funktion
Hallo Pseudemys Nelsoni,
wenn Du Dich intensiver mit Stringverarbeitung auseinandersetzen wirst, könnten reguläre Ausrücke ( ![]() ![]() Eine ad hoc-Lösung für Dein Problem könnte dann zB so aussehen (geht sicher eleganter)
Delphi-Quellcode:
function CountWords(const AString: string; const ADelimiter: Char): Cardinal;
begin Result:= 0; with TRegExpr.Create do try Expression:= Format('[^%s]+', [QuoteRegExprMetaChars(ADelimiter)]); if Exec(AString) then repeat Inc(Result); until not ExecNext; finally Free; end; end; |
Re: Frage zu einer funktion
Ist zwar nicht aus meiner Feder der Code, aber ich hoffe das ist das was du brauchst
Delphi-Quellcode:
function WoerterZaehlen(GesamtString,Sep:String):Integer;
var PropertyListe:TStrings; begin PropertyListe := TStringList.Create; try while AnsiPos(';', GesamtString) > 0 do begin PropertyListe.Add(copy(GesamtString, 1, AnsiPos(Sep, GesamtString)-1)); GesamtString := copy(GesamtString, AnsiPos(Sep, GesamtString) +1, Length(GesamtString)); end; PropertyListe.add(GesamtString); return := PropertyListe.Items.Count; finally PropertyListe.free; end; end; |
Re: Frage zu einer funktion
hoi, danke an alle, sieht gut aus :thuimb:
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 02:47 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz