function ExtractFilePath(const FileName: string): string; var I: Integer; begin I := LastDelimiter(PathDelim + DriveDelim, FileName); Result := Copy(FileName, 1, I); end;
一眼就看到了LastDelimiter,踏破铁鞋无觅处,来得全不费功夫。
终于用了简单的几句实现了getBaseURL函数了。
function getBaseURL(sURL:string):string; begin if Pos('?',sURL)>0 then begin Result:=LeftStr(sURL,Pos('?',sURL)-1); end; Result := Copy(Result, 1, LastDelimiter('/', Result)); end;
LastDelimiter的实现:
function LastDelimiter(const Delimiters, S: string): Integer; var P: PChar; begin Result := Length(S); P := PChar(Delimiters); while Result > 0 do begin if (S[Result] <> #0) and (StrScan(P, S[Result]) <> nil) then {$IFDEF MSWINDOWS} if (ByteType(S, Result) = mbTrailByte) then Dec(Result) else Exit; {$ENDIF} {$IFDEF LINUX} begin if (ByteType(S, Result) <> mbTrailByte) then Exit; Dec(Result); while ByteType(S, Result) = mbTrailByte do Dec(Result); end; {$ENDIF} Dec(Result); end; end;