简体   繁体   中英

Trim URL to Root

How can this be done in Delphi? For example..

URL = https://mail.google.com/mail/u/0/?tab=wm#inbox

Trimed URL = https://mail.google.com/

Thanks

Example code using Indy's TIdURI might be:

uses
  IdURI;

function GetProtoAndHost(const URI: string): string;
var
  IdURI: TIdURI;
begin
  IdURI := TIdURI.Create(URI);
  try
    Result := IdURI.Protocol + '://' + IdURI.Host + '/';
  finally
    IdURI.Free;
  end;
end;
Function GetRoot(const Path:String):String;
var
 i:Integer;
begin
  i := Pos('//',Path);
  if i>0 then
      i := PosEx('/',Path,i+2)
  else i := Pos('/',Path);
  if i=0 then i := Length(Path);

  Result := Copy(Path,1,i);
end;

Have a look at the TIdURI class (in the `IdURI' unit) from Indy. It's a URI/URL parser. You feed it a URL, and it parses it out into the various components. Play around with it and see how it works. Your particular question could be answered by looking at the Host and Protocol properties once it's parsed the URL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM