DelphiDabbler Code Snippets Database

Snippet Selection

The following snippets from the Code Snippets Database have been requested.

DelAllStr

Deletes all occurrences of sub-string Needle from string HayStack and returns the resulting string. HayStack is returned unchanged if it does not contain Needle. The search is case sensitive.

function DelAllStr(const Needle, Haystack: string): string;
var
  StartIdx: Integer;
begin
  Result := Haystack;
  StartIdx := SysUtils.AnsiPos(Needle, Result);
  while StartIdx > 0 do
  begin
    Delete(Result, StartIdx, Length(Needle));
    StartIdx := SysUtils.AnsiPos(Needle, Result);
  end;
end;

DelAllText

Deletes all occurrences of text Needle from text HayStack and returns the resulting string. HayStack is returned unchanged if it does not contain Needle. The search is case insensitive.

function DelAllText(const Needle, Haystack: string): string;
var
  StartIdx: Integer;
  LowerNeedle: string;
begin
  Result := Haystack;
  LowerNeedle := SysUtils.AnsiLowerCase(Needle);
  StartIdx := SysUtils.AnsiPos(LowerNeedle, SysUtils.AnsiLowerCase(Result));
  while StartIdx > 0 do
  begin
    Delete(Result, StartIdx, Length(Needle));
    StartIdx := SysUtils.AnsiPos(LowerNeedle, SysUtils.AnsiLowerCase(Result));
  end;
end;

DelStr

Deletes the first occurrence of sub-string Needle from string HayStack and returns the resulting string. HayStack is returned unchanged if it does not contain Needle. The search is case sensitive.

function DelStr(const Needle, Haystack: string): string;
var
  StartIdx: Integer;
begin
  Result := Haystack;
  StartIdx := SysUtils.AnsiPos(Needle, Result);
  if StartIdx > 0 then
    Delete(Result, StartIdx, Length(Needle));
end;

DelText

Deletes the first occurrence of text Needle from text HayStack and returns the resulting string. HayStack is returned unchanged if it does not contain Needle. The search is case insensitive.

function DelText(const Needle, Haystack: string): string;
var
  StartIdx: Integer;
begin
  Result := Haystack;
  StartIdx := SysUtils.AnsiPos(
    SysUtils.AnsiLowerCase(Needle), SysUtils.AnsiLowerCase(Haystack)
  );
  if StartIdx > 0 then
    Delete(Result, StartIdx, Length(Needle));
end;

View the whole database.

Go to the DelphiDabbler website.