![]() |
Re: Nur ganze Wörter finden
Mit FindText gehte s prima.
Was ist in der Unit StrUtils? |
Re: Nur ganze Wörter finden
Da findet man die Funktion PosEx! Das stand in einem Beitrag hier, der wurde aber editiert...
|
Re: Nur ganze Wörter finden
So, hier mal die ganze Klasse:
Delphi-Quellcode:
Eventuell fallen euch ja noch ein paar nützliche sachen ein, die mit dem RichEdit zutun haben und da rein können. Aber mehr brauche ich im Moment selber nicht.
{************************************************************}
{ } { RichEditHelperCls Unit - Version 1.1 } { } { Copyright (c) 2004 Michael Puff } { } { When I die I want 'Hello, world' carved on my headstone. } { } {************************************************************} unit RichEditHelperCls; interface uses ComCtrls, Classes, Graphics; type TWordDelimiter = set of Char; var WordDelimiter: TWordDelimiter = [#1..#64, #91..#96, #123..#127]; type TRichEditHelper = class(TObject) private FRichEdit: TRichEdit; function GetWords: Integer; public constructor Create(RichEdit: TRichEdit); property CountWords: Integer read GetWords; function Find(StartPos: Integer; const SubStr: string; WholeWord: Boolean): Integer; function HighLightWords(Words: TStrings; FontStyle: TFontStyles; Color: TColor): Cardinal; end; implementation //////////////////////////////////////////////////////////////////////////////// // // TRichEditHelper.Create // // Author : Michael Puff // Date : 2004-08-09 // Purpose: Constructor // constructor TRichEditHelper.Create(RichEdit: TRichEdit); begin inherited Create; FRichEdit := RichEdit; end; //////////////////////////////////////////////////////////////////////////////// // // TRichEditHelper.CountWords // // Author : Michael Puff // Date : 2004-08-08 // Purpose: Ermittelt die Anzahl der Wörter im RichEdit // function TRichEditHelper.GetWords: Integer; var p: PChar; CntWords: Cardinal; begin CntWords := 0; p := PChar(FRichEdit.Text); while p^ <> #0 do begin if p^ in WordDelimiter then Inc(p) else begin while not (p^ in WordDelimiter) do Inc(p); Inc(CntWords); end; end; result := CntWords; end; //////////////////////////////////////////////////////////////////////////////// // // TRichEditHelper.Find // // Author : Michael Puff // Date : 2004-08-08 // Purpose: Findet eine Zeichenfolge im Text des RichEdit // function TRichEditHelper.Find(StartPos: Integer; const SubStr: string; WholeWord: Boolean): Integer; begin if WholeWord then Result := FRichEdit.FindText(SubStr, StartPos, Length(FRichedit.Text) - StartPos, [stWholeWord]) else Result := FRichEdit.FindText(SubStr, StartPos, Length(FRichedit.Text) - StartPos, []); end; //////////////////////////////////////////////////////////////////////////////// // // TRichEditHelper.HighlightWords // // Author : Michael Puff // Date : 2004-08-08 // Purpose: Hebt Wörter aus einer List in einem RichEdit hervor // function TRichEditHelper.HighlightWords(Words: TStrings; FontStyle: TFontStyles; Color: TColor): Cardinal; var i: Integer; LastWordPos: Integer; HighLightedWords: Cardinal; begin LastWordPos := 0; HighLightedWords := 0; FRichEdit.Lines.BeginUpdate; for i := 0 to Words.Count - 1 do begin while LastWordPos <> 1 do begin LastWordPos := Find(LastWordPos, Words[i], True); FRichEdit.SelStart := LastWordPos; FRichEdit.SelLength := length(Words[i]); FRichEdit.SelAttributes.Color := Color; FRichEdit.SelAttributes.Style := FontStyle; LastWordPos := LastWordPos + 2; Inc(HighLightedWords); end; LastWordPos := 0; end; FRichEdit.Lines.EndUpdate; result := HighLightedWords; end; end. |
Alle Zeitangaben in WEZ +1. Es ist jetzt 16:43 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-2025 by Thomas Breitkreuz