delphi,由相對路徑獲得絕對路徑演算法

2021-04-14 01:11:57 字數 3618 閱讀 1923

//

子串出現的次數

function

var

i: integer;

p: integer;

begin

p := pos(sub, st);

i := 0;

whilep > 0do

begin

inc(i);

delete(st, 1, p + length(sub) - 1);

p := pos(sub, st);

end;

result := i;

end;

//按字串分解

functiondecomposestr(sub, st:string;vartst: tstringlist): boolean;

var

num: integer;

p: integer;

begin

p := pos(sub, st);

tst.clear;

whilep > 0do

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;

//返回第幾次前的字元

functioncopyleftnum(sub, st:string; num: integer):string;

var

tst: tstringlist;

i: integer;

begin

tst := tstringlist.create;

decomposestr(sub, st, tst);

ifnum >= tst.count then

result := st

else

begin

fori := 0tonum - 1do

begin

result := result + tst[i];

end;

end;

tst.free;

end;

//返回右邊第幾次出現後的所有字元

function

var

tst: tstringlist;

i: integer;

begin

tst := tstringlist.create; //

在那裡定義那裡釋放

,tstrings

最好傳參

,一定要賦初值

try

decomposestr(sub, st, tst);

result := '';

ifnum < tst.count then

begin

fori := tst.count - numtotst.count - 1do

begin

result := result + tst[i]

end;

end;

finally

tst.free;

end;

end;

//獲得相對路徑的絕對路徑

source

是路徑不要最後的/

functiongetabsolutepath(source, relative:string):string;

var

i, num, num1: integer;

st: tstringlist;

s:string;

begin

'..'

, relative);

st := tstringlist.create;

decomposestr(

'/', source, st);

num1 := st.count;

//刪除後邊的

num次

result := '';

fori := 0tonum1 - num - 1do

begin

result := result + st[i];

end;

'../'

, relative, 1);

result := result + s;

st.free;

end;

呼叫:

st := getabsolutepath(

'f:/jq/myprogram/

城鎮地籍介面/盤龍

/程序',

'../../../vc/delphi/bak/vcp.exe'

);

//閆磊   email:[email protected],[email protected] 2001.10.03編寫

delphi 由絕對路徑獲得相對路徑演算法

取兩個目錄的相對路徑,注意串尾不能是 字元 functiongetrelativepath source,dest string string 交換字串 functiongetpathcomp s1,s2 string integer procedureswapstr vars1,s2 string...

由絕對路徑求相對路徑

最近公司的乙個任務需要修改檔案原來的絕對路徑為相對路徑,於是寫了乙個方法 由絕對路徑獲得相對路徑 path2相對於path1的相對路徑 param path1 path1絕對路徑 param path2 path2絕對路徑 return 相對路徑 throws exception author pu...

相對路徑絕對路徑

前兩天突然發現自己一直以來對相對路徑絕對路徑的理解都是錯的,於是趕緊查了相關資料。1.絕對路徑 絕對路徑是指檔案在硬碟上真正存在的路徑。例如 bg.jpg 這個是存放在硬碟的 e book 網頁布局 第2章 目錄下,那麼 bg.jpg 這個的絕對路徑就是 e book 網頁布局 第2章 bg.jpg...