假如在我们的listbox组件里有这样一些字段,
items 0:(00000|eeee|9999|rrrrr)这是第一行的内容;
items 1:(33333|weww|eeee|eeeee)这是第二行的内容;
这个符号‘|’是用来分开里面的内容的;
现在我们要取的数是:第一次取str1:=00000;str2:=eeee;str3:=9999;str4:=rrrrr;
                    第二次取str1:=33333;str2:=weww;str3:=eeee;str4:=eeeee;
请问这怎么去取。帮我写一下程序好吗?
谢谢了!

解决方案 »

  1.   

    for i:=0 to ListBox1.Items.Count-1 do
        begin
            strTemp:=ListBox1.Items[i];
            str1:=Copy(strTemp,2,Pos('|',strTemp)-2);
            strTemp:=Copy(strTemp,Pos('|',strTemp)+1,Length(strTemp));
            str2:=Copy(strTemp,1,Pos('|',strTemp)-2);
            strTemp:=Copy(strTemp,Pos('|',strTemp)+1,Length(strTemp));
            str3:=Copy(strTemp,1,Pos('|',strTemp)-2);
            strTemp:=Copy(strTemp,Pos('|',strTemp)+1,Length(strTemp));
            str4:=Copy(strTemp,1,Length(strTemp)-1);
        end;
      

  2.   

    楼上写完别人看来都不用写了
    看了以后觉得很到位
    换言之,就算楼主不知道用包括copy,pos在内的各种函数,也应该可以考虑到可以遍历一下这个字符串
    字符串长度N,你只需要N步就能完成你要的功能,再有就是如果你不知道有多少个分段的时候可以考虑动态建立Tstrings,当然,多练,多熟悉,多写才是真正有效的
      

  3.   

    for i:=0 to ListBox1.Items.Count-1 do
        begin
           {  Ip_Point_count := 0;
             STemp := '';
             ListBox1.Items.Clear;
             while Ip_Point_count < 4  do
             begin
                  i :=  pos('|',SGetIpAddr);
                  Ip_Point_count := Ip_Point_count + 1;
                  if (i > 0) then
                  begin
                       STemp := STemp + Copy(SGetIpAddr, 1, I - 1);
                       SGetIpAddr := copy (SGetIpAddr, i + 1, length(SGetIpAddr) - i);
                       i := pos('|', SGetIpAddr);
                  end;         end;
             }
            stmp :=ListBox1.Items[i];
            Edit1.Text:=Copy(stmp,1,Pos('|',stmp)-1);
            stmp:=Copy(stmp,Pos('|',stmp)+1,Length(stmp)-1);
            Edit2.Text:=Copy(stmp,1,Pos('|',stmp)-1);
            stmp:=Copy(stmp,Pos('|',stmp)+1,Length(stmp)-1);
            Edit3.Text:=Copy(stmp,1,Pos('|',stmp)-1);
            stmp:=Copy(stmp,Pos('|',stmp)+1,Length(stmp)-1);
            Edit4.Text:=Copy(stmp,1,Length(stmp));
        end;
      

  4.   

    再给你一段更好一点的代码:procedure TForm1.Button2Click(Sender: TObject);
    var
        i,j:integer;
        str:array[0..3] of string;
        strTemp:string;
        slTemp:TStringList;
    begin
        slTemp:=TStringList.Create;
        for i:=0 to ListBox1.Items.Count-1 do
        begin
            strTemp:=Copy(ListBox1.Items[i],2,Length(ListBox1.Items[i])-3);
            strTemp:=StringReplace(strTemp,'|',#$D#$A,[rfReplaceAll]);
            slTemp.Text:=strTemp;
            for j:=0 to slTemp.Count-1 do
                str[j]:=slTemp.Strings[j];
    ///////////////str[0]:=00000;str[1]:=eeee;str[2]:=9999;str[3]:=rrrrr;
        end;
        slTemp.Free;
    end;
      

  5.   

    提供一个方法function SplitStr(sSource, sSplit: String): TStringList;
    begin
      Result:=TStringList.Create;
      if (Trim(sSource)='') or (Trim(sSplit)='') then 
        Exit;
      while Pos(sSplit,sSource)>0 do
      begin
        Result.Add(Copy(sSource,1,Pos(sSplit,sSource)-1));
        sSource:=Copy(sSource,Pos(sSplit,sSource)+1,Length(sSource));
      end;
      Result.Add(sSource);
    end;