var s:string//部分代码
s:='<li>aaccd</li>//连在一起的,为了看着直观,分开
    <li>cccdddd</li>
    <li>dddadas</li>
    <li>dfads</li>';
我要求s变成:
   '<li>aaccd cccdddd<li>>//连在一起的,为了看着直观,分开
    <li>dddadas dfads</li>'就是通过替换中间的'</li><li>',从而由四个配对的<li></li>变成两个<li></li>,当然不能直接用stringreplace替换,因为偶数次出现的</li><li>不能够替换,
如何搞定这个事情?希望代码简单一些..谢谢.立刻给分.

解决方案 »

  1.   

    我也给个笨方法,呵呵。。
    var
      first:boolean;
      str,temp:string;
      i:integer;
    begin
      first:=true;  //表示奇数或者偶数次找到
      str:=s;
      while pos('</li> <li>',str)<>0 do
      begin
        i:=pos(</li> <li>,str);
        temp :=temp+copy(str,1,i-1); 
        str:=copy(str,i+length(</li> <li>),length(str)-i-length(</li> <li>))
        if  first then
        begin
          first:=false;
        end
        else
        begin
          temp:=temp+'</li> <li>';
          first:=true;
        end;
      end;
      temp:=temp+str;
    end;
      

  2.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      s:string ;
      flag :boolean ;
      temp :String ;
      len :Integer;
    begin
    s:='<li>aaccd </li><li>cccdddd </li><li>dddadas </li><li>dfads </li>';
    temp := '';
    flag := true;
    while pos('</li><li>',s) > 0 do
    begin
     if flag then
        temp := temp +(Copy(s,1,pos('</li><li>',s)-1))
     else
        temp := temp + Copy(s,1,pos('</li><li>',s)-1)+'</li><li>' ;  len := pos('</li><li>',s) + length('</li><li>');
      s:=  Copy(s,len,length(s)-len+1);
      flag := not flag ;
    end; temp := temp + s ; Edit1.Text := temp;end;
      

  3.   

    在给temp附值前最好先setlength一下
    setLength(temp, Length(str));
    要不然的话,每次给temp附值都会超过temp原有大小,然后系统会重新分配空间,这个代价是很高的