Title: Count how many times a word is into a RichEdit
Question: Fast counting words in RichEdit
Answer:
We have an RichEdit with a large text loaded in it, and we want to count how many times a word is in it.
Very easy with the next function:
For use it, simply put a RichEdit (RichEdit1) and a TButton (Button1) in a form:
procedure TForm1.Button1Click(Sender: TObject);
function CuentaPalabrasEnRE(QueRE: TRichEdit; Palabra: string):integer;
var
EncontradoEn : integer;
Largo : integer;
begin
Result:=1;
Largo:=Length(QueRE.Text);
//La primera busqueda es caso especial, dado que si no existe
//la cadena ni una sola vez, pasamos del resto
EncontradoEn:=QueRE.FindText( Palabra,
EncontradoEn,
Largo,
[Stmatchcase]);
if EncontradoEn -1 then begin
repeat
EncontradoEn:= QueRE.FindText( Palabra,
EncontradoEn,
Largo,
[Stmatchcase]);
if EncontradoEn -1 then begin
EncontradoEn:=EncontradoEn+Length(Palabra);
Inc(Result);
end;
until EncontradoEn = -1;
end else Result:=0;
end;
begin
Label1.Caption:=IntToStr( CuentaPalabrasEnRE(RichEdit1,'Eric') );
end;