Conta SubString

Delphi

28/10/2003

Alguém pode me informar se existe alguma função que conta quantas vezes uma subString aparece em uma string;

Se não existir e alguém possuir uma. Preciso muito.

Grato,

Rafael


Biscoitorfa

Biscoitorfa

Curtidas 0

Respostas

Rafael Heise

Rafael Heise

28/10/2003

Faça da seguinte forma:
Function CountSubString(aString, aSubString: string; aCaseSensitive: boolean = False): integer;
// Create by Rafael M. Heise
// in 28/10/2003 (dd/mm/yyyy)
var
  count: integer;
begin
  if not aCaseSensitive then // if not use CaseSentive
  begin // all to upper
    aString := AnsiUpperCase(aString);
    aSubString := AnsiUpperCase(aSubString);
  end;
  count := 0;
  while Trim(aString) <> ´´ do // while text is not null
  begin
    if Pos(aSubstring,aString) > 0 then // if found sub-text in text
    begin
      Inc(count); // inc count
      Delete(aString,1,Pos(aSubString,aString) + Length(aSubString)); // delete text verified
    end
    else // if not found, clear text to exit
      aString := ´´;
  end;
  Result := count; // return count
end;


só peço que deixe os comentários...


GOSTEI 0
POSTAR