//子串出现的次数
function GetappearNum(sub, st: string): integer;
var
i: integer;
P: integer;
begin
p := Pos(sub, st);
I := 0;
while p > 0 do
begin
inc(i);
delete(st, 1, p + length(sub) - 1);
p := Pos(sub, st);
end;
result := i;
end;
//按字串分解
function decomposestr(sub, st: string; var tst: TStringList): Boolean;
var
num: integer;
P: integer;
begin
p := Pos(sub, st);
tst.Clear;
while p > 0 do
begin
num := p + length(sub) - 1;
tst.Add(copy(st, 1, num));
delete(st, 1, num);
p := Pos(sub, st);
end;
tst.Add(st);
Result := True;
end;
//返回第几次前的字符
function CopyLeftNum(sub, st: string; num: integer): string;
var
Tst: TStringList;
I: integer;
begin
tst := TStringList.Create;
decomposestr(sub, st, Tst);
if Num >= Tst.Count then
Result := st
else
begin
for i := 0 to num - 1 do
begin
Result := Result + Tst[i];
end;
end;
Tst.Free;
end;
//返回右边第几次出现后的所有字符
function CopyRightNum(sub, st: string; Num: integer): string;
var
Tst: TStringList;
I: integer;
begin
Tst := TStringList.Create; //在那里定义那里释放,tstrings最好传参,一定要赋初值
try
decomposestr(sub, st, Tst);
Result := '';
if Num < Tst.Count then
begin
for i := Tst.Count - Num to Tst.Count - 1 do
begin
Result := Result + Tst[i]
end;
end;
finally
Tst.Free;
end;
end;
//获得相对路径的绝对路径 Source是路径不要最后的/
function GetabsolutePath(Source, Relative: string): string;
var
i, Num, num1: integer;
St: TStringList;
s: string;
begin
Num := GetappearNum('..', Relative);
St := TStringList.Create;
decomposestr('/', Source, st);
Num1 := st.Count;
//删除后边的NUM次
Result := '';
for i := 0 to num1 - num - 1 do
begin
Result := Result + st[i];
end;
s := CopyRightNum('../', Relative, 1);
Result := Result + s;
st.Free;
end;
调用:
st := GetabsolutePath('F:/jq/myprogram/城镇地籍界面/盘龙/进程', '../../../vc/delphi/BAK/vcp.exe');
//闫磊 Email:Landgis@126.com,yanleigis@21cn.com 2001.10.03编写