DelphiDabbler Code Snippets Database

Snippet Selection

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

ExtractURIFragment

Returns any fragment component of the given URI. Returns the empty string if the URI has no fragment.

function ExtractURIFragment(const URI: string): string;
var
  FragmentStart: Integer;
begin
  FragmentStart := SysUtils.AnsiPos('#', URI);
  if FragmentStart > 0 then
    Result := Copy(URI, FragmentStart + 1, Length(URI) - FragmentStart)
  else
    Result := '';
end;

ExtractURIQueryString

Returns any query string component of a given URI. Returns the empty string if the URI has no query string.

function ExtractURIQueryString(const URI: string): string;
var
  QueryStart: Integer;
  QueryEnd: Integer;
begin
  Result := '';
  QueryStart := SysUtils.AnsiPos('?', URI);
  if QueryStart = 0 then
    Exit;
  Inc(QueryStart);
  QueryEnd := SysUtils.AnsiPos('#', URI);
  if QueryEnd < QueryStart then
    QueryEnd := Length(URI)
  else
    Dec(QueryEnd);
  Result := Copy(URI, QueryStart, QueryEnd - QueryStart + 1);
end;

RemoveURIFragment

Removes any fragment part from the end of the given URI and returns the result. The URI is returned unchanged if it has no fragment.

function RemoveURIFragment(const URI: string): string;
var
  FragmentStart: Integer;
begin
  FragmentStart := SysUtils.AnsiPos('#', URI);
  if FragmentStart > 0 then
    Result := Copy(URI, 1, FragmentStart - 1)
  else
    Result := URI;
end;

RemoveURIQueryString

Removes any query string from the given URI and returns the result. The URI is returned unchanged if it has no query string.

function RemoveURIQueryString(const URI: string): string;
var
  QueryStart: Integer;
  FragStart: Integer;
begin
  QueryStart := SysUtils.AnsiPos('?', URI);
  if QueryStart = 0 then
  begin
    Result := URI;
    Exit;
  end;
  Result := Copy(URI, 1, QueryStart - 1);
  FragStart := SysUtils.AnsiPos('#', URI);
  if FragStart > 0 then
    Result := Result + Copy(URI, FragStart, Length(URI) - FragStart + 1);
end;

View the whole database.

Go to the DelphiDabbler website.