listbox上的数据每次取10条连成一个串,不足10条时也组成一个串,放到另一个listbox上
如:
a1
a2
a3
a4
a5
a6
a7
a8
a9
a10
a11
a12在另一个listbox上:
a1a2a3a4a5a6a7a8a9a10
a11a12

解决方案 »

  1.   

    var
    i,j:integer;
    str:string;
    begin
     for i:=1 to (listbox1.Items.Count div 10 )+1 do
     begin
       str:='';
       for j:=(i-1)*10 to (i*10-1)  do
       begin
        if j<listbox1.Items.Count then
        str:= str+listbox1.Items[j];
       end;
       listbox2.Items.Add(str); end;
    end;
      

  2.   

    procedure TForm1.FormShow(Sender: TObject);
    var i:integer;
    begin
       for i:=0 to 35 do
          listbox1.Items.Add('a'+inttostr(i));
    end;procedure TForm1.Button1Click(Sender: TObject);
    var a:string;
    i:integer;
    begin
       for i:=1 to 30 do
            if i mod 10<>0 then
               begin
                a:=a+listbox1.Items[i];
               end
            else  if i mod 10=0 then
                begin
                  listbox2.Items.Add(a);
                  a:='';
                end;
    end;
      

  3.   

    用循环
    j:=0;
    while j<ListBox1.Items.Count do
    begin
      vStr:='';
      for i:=0 to 9 do
      begin
        if j=ListBox1.Items.Count then  不足10个退出
          Break;
        vStr:=vStr+ListBox1.Items[j];
        Inc(j);
      end;
      ListBox2.Items.Add(vStr);
    end;
      

  4.   

    再请教一下:
    COM1COM2COM3COM4COM7COM9假如上面是listbox上的一项,请问怎么获得该字符串有几个'COM'字符?
    函数应返回6
    怎么写
      

  5.   

    我在用的一个比较原始的方法:
    function GetStrCount(vSource,vKey:String):Integer;
    var
      vCount:integer;
      vTmpSource:String;
    begin
      vTmpSource:=vSource;
      vCount:=0;
      while Pos(vKey,vTmpSource)<>0 do
      begin
        vTmpSource:=Copy(vTmpSource,Pos(vKey,vTmpSource)+1,Length(vTmpSource));
        Inc(vCount);
      end;
      Result:=vCount;
    end;
      

  6.   

    这是个优化后的方法,希望大家指导指导.
    function GetStrCount(vSource,vKey:String):Integer;
    var
      vCount:integer;         //最后结果
      vTmpSource:TStrings;    
      vTmpString:String;
    begin
      vTmpSource:=TStringList.Create;
      vTmpString:=vSource;   //做个备份
      vTmpSource.Text:=StringReplace(vTmpString,vKey,#13#10,[rfReplaceAll, rfIgnoreCase]);
      //这里是关键,把关键字改成回车放到TStrings里,TSTrings的个数就是关键字的个数.
      Result:=vTmpSource.Count;
    end;