我常用的几个自写的字符串处理函数
unit StrSub;
interface
uses
windows, SysUtils, StrUtils;
function StrToUrlStr(s:String):string;
//获取SubStr在Str中左边的部分字符串,从左起搜索
function GetLeftStr(SubStr, Str: string): string;
//获取SubStr在Str中右边的部分字符串,从左起搜索
function GetRightStr(SubStr, Str: string): string;
//获取SubStr在Str中左边的部分字符串,从右起搜索
function GetLeftEndStr(SubStr, Str: string): string;
//获取SubStr在Str中右边的部分字符串,从右起搜索
function GetRightEndStr(SubStr, Str: string): string;
implementation
function GetLeftStr(SubStr, Str: string): string;
begin
Result := Copy(Str, 1, Pos(SubStr, Str) - 1);
end;
//-------------------------------------------
function GetRightStr(SubStr, Str: string): string;
var
i: integer;
begin
i := pos(SubStr, Str);
if i > 0 then
Result := Copy(Str, i + length(substr), length(Str))
else
Result := '';
end;
//---------------------------------------------
function GetLeftEndStr(SubStr, Str: string): string;
var
i: integer;
S: string;
begin
S := Str;
Result := '';
repeat
i := Pos(SubStr, S);
if i > 0 then
begin
if Result <> '' then
Result := Result + SubStr + GetLeftStr(SubStr, S)
else
Result := GetLeftStr(SubStr, S);
S := GetRightStr(SubStr, S);
end;
until i <= 0;
end;
//-------------------------------------------
function GetRightEndStr(SubStr, Str: string): string;
var
i: integer;
begin
Result := Str;
repeat
i := Pos(SubStr, Result);
if i > 0 then
begin
Result := GetRightStr(SubStr, Result);
end;
until i <= 0;
end;
//-------------------------------------------
在以上几个函数的基础上解决楼主的问题
path2 := 'C:/dat/mytxt/b.txt';
path1 := '../map/a.txt'
function GetPath(path1,path2:string):string;
var
ts : string;
Begin
result:= GetLeftEndStr('/',path2);
While path1<>'' do
BEGIN
if Pos('/',path1)>0 then
begin
ts := GetLeftStr('/',path1);
path1 := GetRightStr('/',path1);
end
begin
ts := path1 ;
path1 := '';
end;
if ts='..' then
result:= GetLeftEndStr('/',result)
else if ts ='.' then continute
else
begin
result:= result+ '/' +ts;
end;
end;
我不知道有没有现成的函数,呵呵,手写的,没有测试过.
ExtractFilePath(Path2) + Path1 会得到下面的结果
'C:/dat/mytxt/../map/a.txt'
这个执行大部分操作还是没什么问题的,不过好像有些用这样的路径会出错