function Fenli(Src: string; Before, After: string): string;//字符串分离函数
var
Pos1, Pos2: Word;
Temp: string;
begin //使用方法:Fenli('www.zhuanhou.cn','www.','.cn') 得到的结果为:zhuanhou
Temp := Src;
Pos1 := Pos(Before, Temp);
delete(Temp, 1, Pos1+length(Before));
Pos2 := Pos(After, Temp);
if (Pos1 = 0) or (Pos2 = 0) then
begin
result := '';
Exit;
end;
Pos1 := Pos1 + Length(Before);
result := Copy(Src, Pos1, Pos2);
end;请问这个函数为什么在删除的时候还要加上length(Before). Pos1 不是已经包含了完整的长度了吗? 我用我的代码测试了,是可以,但是这个也可以,我真的看不懂。有本身的长度,还要加上Before 参数的长度。

解决方案 »

  1.   

    你试试不就知道了吗
    Pos1 := Pos('www.', 'www.zhuanhou.cn');
    结果是1,
    delete('www.zhuanhou.cn', 1, Pos1+length(Before));
    如果不加length('www.')变为
    delete('www.zhuanhou.cn', 1, 1);
    你觉得这样对吗
      

  2.   

    按你上面的例子,Pos1 := Pos(Before, Temp);這句返回值是1,所以你刪除語句需要用需刪除的字串長度。
    delete(Temp, 1, Pos1+length(Before));如果這句“.“沒空格的話也是錯的,正確的是:
    delete(Temp, 1, length(Before));從第一個字符開始刪除length(Before)個字符。
      

  3.   


    function Fenli(Src: string; Before, After: string): string;//字符串分离函数
    var
    Pos1, Pos2: Integer;
    begin //使用方法:Fenli('www.zhuanhou.cn','www.','.cn') 得到的结果为:zhuanhou
    Pos1 := Pos(Before, Src);
    Pos2 := Pos(After, Src);
    if (Pos1 = 0) or (Pos2 = 0) then
    begin
    result := '';
    Exit;
    end;
    result := Copy( Src, 1, Pos2-1);
    delete(Result, 1, length(Before));
    end;
      

  4.   

    POS得到的是第一个字符的位置,不是最后一个,调试一下就知道了
      

  5.   

    现在我上传另外一个代码。  这是对网页进行解析的。
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls,UrlMon;type
      TForm1 = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
        Edit2: TEdit;
        Memo1: TMemo;
        Edit3: TEdit;
        Label1: TLabel;
        Edit4: TEdit;
        Edit5: TEdit;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    function DownloadFile(SourceFile, DestFile: string): Boolean; //下载函数
    begin //可以下载网页,图片等任何文件,如 上海门禁系统
    try
        result := UrlDownloadToFile(nil,PChar(SourceFile),PChar(DestFile), 0, nil) = 0;
       except
        result := False;
       end;
    end;
    function Fenli1(Src: string; Before, After: string): string; //字符串分离函数
    var
    Pos1, Pos2: Word;
    Temp: string;
    begin //使用方法:Fenli('www.zhuanhou.cn','www.','.cn') 得到的结果为:zhuanhou
    Temp := Src;
    Pos1 := Pos(Before, Temp);
    delete(Temp, 1, Pos1);
    Pos2 := Pos(After, Temp);
    if (Pos1 = 0) or (Pos2 = 0) then
    begin
    result := '';
    Exit;
    end;
    Pos1 := Pos1 + Length(Before);
    form1.edit1.Text := Copy(Src, Pos1, Pos2);
    end;
    function Fenli(Src: string; Before, After: string): string;//字符串分离函数
    var
    Pos1, Pos2: Word;
    Temp: string;
    begin //使用方法:Fenli('www.zhuanhou.cn','www.','.cn') 得到的结果为:zhuanhou
    Temp := Src;
    Pos1 := Pos(Before, Temp);
    form1.Edit4.Text:=inttostr(pos1+length(Before));
    delete(Temp, 1, Pos1+length(Before));
    form1.Edit5.Text:=temp;
    Pos2 := Pos(After, Temp);
    if (Pos1 = 0) or (Pos2 = 0) then
    begin
    result := '';
    Exit;
    end;
    Pos1 := Pos1 + Length(Before);
    form1.edit1.Text := Copy(Src, Pos1, Pos2);
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    var
       i:Integer;
       LadTxt:TstringList;
    begin
    //下载文件,我认为这种方法下载的文件其实就相当于webbrowser访问这个地址
    //IP.txt 为保存的文件
    DownloadFile('http://www.ip138.com/ips.asp?ip='+edit3.text+'&action=2','IP.txt');
    LadTxt:=TStringList.Create;
    LadTxt.LoadFromFile('IP.txt');
    memo1.Lines.LoadFromFile('IP.txt');
    //循环读取 IP.txt 文件
    for i:=0 to LadTxt.Count -1 do
       begin
         //如果当前行包含 您的IP地址是:并且包含 ]
         if (Pos('本站主数据:',LadTxt[i])>0) and (Pos('参',LadTxt[i])>0) then
          begin
          edit2.Text:=trim(ladtxt[i]);
           edit1.Text:=fenli(trim(LadTxt[i]),':','<');    //赋值 分离得到当前IP地址
           Text:='获取外网IP地址成功IP地址为'+edit1.Text;//更改标题
           Exit;                                 //退出循环
          end;
       end;
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
     Fenli('www.zhuanhou.cn','www.','.cn');
    end;end.
      

  6.   

    Pos1 := Pos(Before, Temp);获得的是Before在Temp中出现的位置 ,比如 Before := 'AA' ;
    Temp :=  'BAAC' ;Pos1 := Pos(Before, Temp);  //Pos1:= 2  ; 得到的是Before在Temp中出现的第一个位置,即AA中的第一个A出现的位置 2 ,delete(Temp, 1, Pos1+length(Before)); //从Temp中删除掉Before 和Before之前的字符 即 结果为 C如果使用:delete(Temp, 1, Pos1); //得到的结果是AC  ;
      

  7.   

    DELPHI的是临时变量TEMP的。 delete(Temp, 1, Pos1+length(Before));
    后面的COPY是 传进去参数 Src的, 你说要不要加? Pos1 := Pos1 + Length(Before); 
    Copy(Src, Pos1, Pos2);
    POS2的变量值在SRC其实是 POS2- POS1 - length(Before)
    其实你Copy(Temp, 1, Pos2 - 1); 就是你要的值 zhuanhou
      

  8.   

     cai5 兄弟。
    我测试过的。 得到在字符串内的长度,也包涵Before 的。pos1+Before=4.
    delete(temp,1,4)  直接就没有了var
    pos1:integer;
    Before:string;
    Temp:string;
    begin
    Before:= 'AA';
    temp:='BAAC' ;
    Pos1 := Pos(Before, Temp);
    edit1.Text:=inttostr(pos1);
    delete(Temp, 1,Pos1+length(Before));
    edit2.Text:=temp;end;测试完成。编辑框2获取到的是空的。
    pos1+length(Before)加在一起是4
     所以delete(temp,1,4);
    直接就没有了。来点专业的回答。 POS2的变量值在SRC其实是 POS2- POS1 - length(Before)  当然是他了。pos2是temp-pos1-lengt(Before)得到的长度
    得到
    其实你Copy(Temp, 1, Pos2 - 1); 这句简直就是胡扯   
      

  9.   

    LZ自己不了解, 脾气倒不小function Fenli(Src: string; Before, After: string): string;//字符串分离函数
    var
      Pos1, Pos2: Word;
      Temp: string;
    begin //使用方法:Fenli('www.zhuanhou.cn','www.','.cn') 得到的结果为:zhuanhou
      Result := '';
      Temp := Src;
      Pos1 := Pos(Before, Temp);
      delete(Temp, 1, length(Before));
      Pos2 := Pos(After, Temp);
      if Pos2> 0 then
        Result := Copy(Temp, 1, Pos2 - 1);
    end;你deleite 前面不知道加个pos1干嘛,明显会多删一个。
    其实就4行代码的东西被你弄这么复杂。
      

  10.   

    不是,如果后面是wwww这样的话,就会删除错误,我试过了,谢谢了。
      

  11.   


    function GetStr(Str: string; StartTag, EndTag: string): string;
    var
      StartIndex, EndIndex: Integer;
    begin
      Result := EmptyStr;
      StartIndex := Pos(StartTag, Str);
      if StartIndex = 0 then Exit;
      Inc(StartIndex, Length(StartTag));
      EndIndex := Pos(EndTag, Copy(Str,StartIndex + 1,Length(Str)-StartIndex)) + StartIndex;
      Result := Copy(Str, StartIndex, EndIndex - StartIndex);
    end;