delphi,由相对路径获得绝对路径算法

 

 

//子串出现的次数

 

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编写

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值