Count substrings within a string

Asked

Viewed 1,882 times

2

I want to know how many times you leave a string appears within a text, example:

In the Microsoft quality is prioritized, therefore the Microsoft is the best.

I want a function in which I pass two parameters and return how many times the first parameter occurs in the second.
Something like:

ContarOcorrencias('Microsoft',TextoOriginal) 

Must return 2 (two occurrences of the word Microsoft).

I have some ideas on how to set up the function, but I wanted to see if there is something native or the best way to create it.

4 answers

3


You can use the method Posex, which returns the position of the index (based on 1, not 0) which substring starts, if no valid substring is found it returns zero.

Hence, the implementation would be something like this:

  1. Occurrences = 0;
  2. Position = Posex (text);
  3. While Position > 0, increments Occurrences and Position = Posex(subtext from Position + size of the searched word);

The algorithm would look something like this:

function Ocorrencias(TextoProcurado: string, Texto: string): integer;
var
  posicao: integer;
begin
  result := 0;
  posicao := PosEx(TextoProcurado, Texto, 1);
  while posicao > 0 do
  begin
    inc(result);
    posicao := PosEx(TextoProcurado, Texto, posicao + length(TextoProcurado));
  end;
end;

2

Several answers, but none simple to solve.

Declare nas uses System.RegularExpressions, then to use :

TRegEx.Matches(TextoOriginal, 'Microsoft').Count

Within the uses I mentioned several other interesting functions!

  • He did not specify which version of Delphi was using, if the one he’s using is Delphi 7 for example, this option will not work.

  • Exactly why I have already posted a more up-to-date answer, which doesn’t use so much the processor! As the tag is delphi, I didn’t cling to details!

  • Only it is not necessary to underestimate all other answers, since yours does not meet all possibilities.

  • There was no disregard my dear, the purpose here is always to answer! And based on something, if you wish to follow the subject, please call in Chat!

1

I believe you have nothing native for this purpose, but you can use the following function:

function CountInText(Find, Text: String): Integer;
begin
  Result := 0;
  while (Pos(Find, Text) > 0) do
    begin
      Text := Copy(Text, 1, Pos(Find, Text) - 1) + Copy(Text, Pos(Find, Text) + Length(Find), Length(Text));
      Inc(Result, 1);
    end;
end;

1

Try it this way:

procedure TForm1.Button1Click(Sender: TObject);
var AWord, AText: String;
begin
  AText := 'Na Microsoft prioriza-se a qualidade, por isso a Microsoft é a melhor.';
  AWord := 'Microsoft';
  Memo1.Lines.Add('Ocorrencias: ' + IntToStr(ContarOcorrencias(AWord, AText)));
end;

Function TForm1.ContarOcorrencias(AWord, AText: String): Integer;
var APos: Integer;
    AWordFind: String;
begin
  Result := 0;

  //enquanto o texto tiver texto verifica
  While AText <> '' do
    Begin
      //procuro espaços o próximo espaço " "
      APos := Pos(' ', AText);
      if (APos > 0) then
        begin
          //pego a palavra e apago-a da variável AText
          AWordFind := Trim(AnsiMidStr(AText, 1, APos - 1));
          Delete(AText, 1, APos);
        end
      //quando não encontramos mais " " então pegamos o resto do texto
      else AWordFind := AText;

      if AWordFind = AWord then Result := Result + 1;
    End;
End;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.