Count substrings within a string

2

I want to know how many times part of a string appears inside a text, example:

  

Microsoft gives priority to quality, so Microsoft is the   better.

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

ContarOcorrencias('Microsoft',TextoOriginal) 

You should 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 anything native or how best to create it.

    
asked by anonymous 03.10.2017 / 18:31

4 answers

3

You can use the PosEx method, which returns the index position (based on in 1, not 0) that substring starts, if it does not find any valid substring it returns zero.

Hence, the implementation would look something like this:

  • Occurrences = 0;
  • Position = PosEx (text);
  • While Position > 0, increments Occurs and Position = PosEx (subtext from Position + word size searched);
  • The algorithm looks 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;
    
        
    03.10.2017 / 18:43
    2

    Several answers, but none simple to solve.

    Declare in uses System.RegularExpressions , then use:

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

    Within the uses I mentioned several other interesting functions!

        
    03.10.2017 / 19:06
    1

    I do not think you have anything 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;
    
        
    03.10.2017 / 18:40
    1

    Try this:

    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;
    
        
    03.10.2017 / 18:41