Einzelnen Beitrag anzeigen

Benutzerbild von KodeZwerg
KodeZwerg

Registriert seit: 1. Feb 2018
3.685 Beiträge
 
Delphi 11 Alexandria
 
#151

AW: Anzahl eines Zeichens im String ermitteln

  Alt 16. Jul 2018, 20:47
Hab noch etwas gebastelt aber ist auch nicht sehr effektiv.
Delphi-Quellcode:
// versuche 4 compares pro loop abzufertigen
function TCharInString.MultiCountChar(const s: string; const c: Char): Integer;
var
  i, ii, iii, iiii: Integer;
begin
  Result := 0;
  ii := Length(s);
  iii := (ii div 2);
  iiii := iii+1;
  for i := 1 to (Length(s) div 2) do
  begin
    if s[i] = c then Inc(Result, 1); // suche vom Anfang in Richtung Mitte
    if s[ii] = c then Inc(Result, 1); // suche vom Ende in Richtung Mitte
    if s[iii] = c then Inc(Result, 1); // suche von Mitte in Richtung Anfang
    if s[iiii] = c then Inc(Result, 1); // suche von Mitte in Richtung Ende
    Dec(ii);
    Dec(iii);
    Inc(iiii);
    if i = iii then Break;
  end;
end;
Gruß vom KodeZwerg
  Mit Zitat antworten Zitat