首先。我得到的一串字符串是这个
http://forum.csdn.net/PointForum/Forum/PostTopic.aspx
我想要的是效果是
http://forum.csdn.net/PointForum/Forum/PostTopic.aspx
http://forum.csdn.net/PointForum/Forum/
http://forum.csdn.net/PointForum/
http://forum.csdn.net/
..不知道大伙明白不......我已经通过百度搜索了得到了一些按照特殊字符分隔号,
但我写的代码得到了像上面的地址....
procedure TForm1.Button4Click(Sender: TObject);
 var
 a,b,c,aa,str:string;
 i,ii:integer;
begin
str := 'http://localhost/blog/delphi/post/delphi字体.html';
a:=str;
aa:='';
delete(a,1,pos('/',a)+1);          //  --删除第一个/的起始,也就是http:/ + /
b:=copy(a,1,pos('/',a)-1);          //   --搜索/ ,因为上面的已经删除了前面的/,
b:='http://'+b;                  //    --所以这一次就从地址的xx.xx.com/为结束得到网址完整
  for i := 3 to pos('/',str) do
    begin
    a:=GetListSubValue(str,'/',i); //getlistsbuvalue是网上的一个分割参数
    aa:=aa+'/'+a;
    memo1.Lines.Add(b+aa);
    end;
这是分割处的代码function GetListSubValue(TotalValue: string;SplitStr: string;subIndex: integer): string;
var
  iPos: integer;
Label 1;
begin
  1:
  iPos := Pos(SplitStr,TotalValue);
  if ipos = 0 then  result := TotalValue
  else if subindex = 0 then
    result := Copy(TotalValue,1,ipos-1)
  else
  begin
    TotalValue := Copy(TotalValue,ipos+Length(SplitStr),MaxInt);
    if subIndex <> 0 then
    begin
       subIndex := subIndex - 1;
       goto 1;
    end;
  end;
  end;
但是虽然能得到了值。但是无论我换了几层目录都是会返回四个目录的数.
正常的目录
http://forum.csdn.net/PointForum/Forum/PostTopic.aspx
执行后
http://forum.csdn.net/PointForum/Forum/PostTopic.aspx/PostTopic.aspx  //多出了一个
http://forum.csdn.net/PointForum/Forum/PostTopic.aspx
http://forum.csdn.net/PointForum/Forum/
http://forum.csdn.net/PointForum/
http://forum.csdn.net/
--------------------------------------------------------------------------我知道是我的算法有问题。所以想请各位朋友能帮帮忙,帮我改一下。就是无论这个地址是什么值。他就会按照目录多少次来进行得到他的目录.....

解决方案 »

  1.   

    var
      temp:string;
      Index:Integer;
      tempHead:string;
    begin
      temp:='http://forum.csdn.net/PointForum/Forum/PostTopic.aspx';
      tempHead:='http://';
      temp:=copy(temp,8,length(temp)-7);
      Index:=pos('/',temp);
      while index>0 do
      begin
        tempHead:=tempHead+copy(temp,1,index);
        listbox1.Items.Add(tempHead);
        temp:=copy(temp,index+1,length(temp));
        Index:=pos('/',temp);
      end;
      listbox1.Items.Add(tempHead+temp);
    end;
      

  2.   

    用下面的GetURLList函数吧。
    procedure GetURLList(URL: String;  var AList: TStringList);
    var
      s : String;
      i : Integer;
    begin
      AList.Clear;
      i := Pos('://', URL);
      if i > 0 then
      begin
        s := Copy(URL, 1, i + 2);
        URL := Copy(URL, i + 3, Length(URL) - i - 2);
      end;
      repeat
        AList.Add(s + URL);
        i := Length(URL);
        while i > 0 do
        begin
          if URL[i] = '/' then
          begin
            URL := Copy(URL, 1, i - 1);
            break;
          end;
          dec(i);
        end;
      until i = 0;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      AList : TStringList;
    begin
      AList := TStringList.Create;
      try
        GetURLList ('http://forum.csdn.net/PointForum/Forum/PostTopic.aspx', AList);
        Memo1.Lines.AddStrings(AList);
      finally
        AList.Free;
      end;
    end;
      

  3.   

    procedure TTestForm.btnTestClick(Sender: TObject);
    var    strList : TStringList;
        I : Integer;
        strUrl : String;
    begin
        strList := TStringList.Create;    strList.Delimiter := '/';
        strList.DelimitedText :='http://forum.csdn.net/PointForum/Forum/PostTopic.aspx';
        strUrl := strList[0] + '//';
        for i := 2 to strList.Count - 2 do
        begin
        strUrl := strUrl + strList[I] + '/';
        Memo1.Lines.Add(strUrl);
        end;
        Memo1.Lines.Add(strList.DelimitedText);end;
      

  4.   

    procedure GetURLList(URL: String;  var AList: TStringList);
    var
      s : String;
      i : Integer;
    begin
      AList.Clear;
      i := Pos('://', URL);
      if i > 0 then
      begin
        s := Copy(URL, 1, i + 2);
        URL := Copy(URL, i + 3, Length(URL) - i - 2);
      end;
      repeat
        AList.Add(s + URL);
        i := Length(URL);
        while i > 0 do
        begin
          if URL[i] = '/' then
          begin
            URL := Copy(URL, 1, i - 1);
            break;
          end;
          dec(i);
        end;
      until i = 0;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      AList : TStringList;
    begin
      AList := TStringList.Create;
      try
        GetURLList ('http://forum.csdn.net/PointForum/Forum/PostTopic.aspx', AList);
        Memo1.Lines.AddStrings(AList);
      finally
        AList.Free;
      end;
    end;procedure TTestForm.btnTestClick(Sender: TObject);
    var    strList : TStringList;
        I : Integer;
        strUrl : String;
    begin
        strList := TStringList.Create;    strList.Delimiter := '/';
        strList.DelimitedText :='http://forum.csdn.net/PointForum/Forum/PostTopic.aspx';
        strUrl := strList[0] + '//';
        for i := 2 to strList.Count - 2 do
        begin
        strUrl := strUrl + strList[I] + '/';
        Memo1.Lines.Add(strUrl);
        end;
        Memo1.Lines.Add(strList.DelimitedText);end;
      

  5.   

    var
      temp:string;
      Index:Integer;
      tempHead:string;
    begin
      temp:='http://forum.csdn.net/PointForum/Forum/PostTopic.aspx';
      tempHead:='http://';
      temp:=copy(temp,8,length(temp)-7);
      Index:=pos('/',temp);
      while index>0 do
      begin
        tempHead:=tempHead+copy(temp,1,index);
        listbox1.Items.Add(tempHead);
        temp:=copy(temp,index+1,length(temp));
        Index:=pos('/',temp);
      end;
      listbox1.Items.Add(tempHead+temp);
    end;
      

  6.   

    比较简单的一种方法:
    procedure TForm1.Button1Click(Sender: TObject);
    var
     str:string;
     i:integer;
    begin
     str:='http://forum.csdn.net/PointForum/Forum/PostTopic.aspx';
     showmessage(str);
     i:=LastDelimiter('/',str);//取得最后出现“/”的位置。
     while i>0 do
      begin
        str:=copy(str,1,i-1);//截掉字符,直接取得上级网址。
        if SameText(str,'http:/') then break;
        showmessage(str);
        i:=LastDelimiter('/',str);
      end;
    end;你可以把网址换成其它的,照样可以。
      

  7.   

    你的循环里错了for i := 3 to pos('/',str) do 
    你看他是从第三位开始循环,到第一个邪干出现的位置。那么他就只循环了这么多次。
    所以当你换了其他的url的话他也是这么多呀。所以你必须要求得他的个数然后才能让程序自己开始循环到目标次数呀。
    function geturlshu(strurl:string):integer;      //我写的一个求得'/'个数的
    shuzu:array [1..100] of string;
     shu:integer;
    str,str1:string;
    begin
       str:=strurl;
       shu:=0;
       str1:=str;
       while pos('/',str1)>0 do
       begin
         shuzu[shu]:=copy(str1,1,pos('/',str1)-1);
         str1:=copy(str1,pos('/',str1)+1,length(str1)-pos('/',str1));
         shu:=shu+1;
       end;
       result:=shu;
    end;你将他代入到你的循环里面去
    ii:=geturlshu(str);         //得到多少'/'个数
      for i := 3 to ii do就可以了。你试试