我写了一段程序,大意是这样的:
统计stringgrid里面第三列每一行的字符串长度(不统计小数点),然后截取edit7里面的相应个数字符串复制给第四列的同一行.
代码如下:
procedure TForm1.btn4Click(Sender: TObject);
var
  row:Byte;
  SS:string;
begin
         SS:=trim(edit7.text);
         for row:=1 to  StringGrid1.RowCount+1 do
         begin
  StringGrid1.Cells[4,row]:= Copy(SS,1,(Length(Deld(StringGrid1.Cells[3,row]))));
  SS:=Copy(SS,Length(Deld(StringGrid1.Cells[3,row]))+1,(Length(SS)-Length(Deld((StringGrid1.Cells[3,row])))));
         endend;
===================================================
function Deld(S:string):string;  
var
  i,j:Byte;
begin
    for j:=1 to length(s) do
        begin
       if copy(s,j,1)<>'.' then
        begin
        s:=s+copy(s,j,1);
        end;
     end;
     Result:=s;
end;
=====================================================
可是当我的[3,row]数据为11.22,33.44,55.66 
              edit7数据为11 22 33 44 55 66  时
            [4,row]数据变成了11 22 33
                              44 55 66
请问这是怎么回事呢?
            我理想的[4,row]数据应该是1122
                                        3344
                                        5566

解决方案 »

  1.   

    顺路说一下,我直接做的EXE,不能单步调试,一把红叉...奇怪
      

  2.   

    function Deld(S:string):string;
    var
      i,j:Byte;
      str:string;
    begin
        str:='';
        for j:=1 to length(s) do
            begin
          if copy(s,j,1) <>'.' then
            begin
            str:=str+copy(s,j,1);
            end;
        end;
        Result:=s;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      row:Byte; 
      SS:string;
      str:string;
    begin
            SS:=trim(edit7.text);
            str:=ss;
            for row:=1 to  StringGrid1.RowCount+1 do
            begin
      StringGrid1.Cells[4,row]:= Copy(ss,1,(Length(Deld(StringGrid1.Cells[3,row]))));
      ss:=Copy(ss,Length(Deld(StringGrid1.Cells[3,row]))+2,(Length(SS)-Length(Deld((StringGrid1.Cells[3,row])))));
            endend;
      

  3.   


    function Deld(S:string):string;
    var
      i,j:Byte;
      str:string;
    begin
        str:='';
        for j:=1 to length(s) do
            begin
          if copy(s,j,1) <>'.' then
            begin
            str:=str+copy(s,j,1);
            end;
        end;
        Result:=s;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      row:Byte;
      SS:string;
      //str:string;
    begin
            SS:=trim(edit7.text);
            //str:=ss;
            for row:=1 to  StringGrid1.RowCount+1 do
            begin
      StringGrid1.Cells[4,row]:= Copy(ss,1,(Length(Deld(StringGrid1.Cells[3,row]))));
      ss:=Copy(ss,Length(Deld(StringGrid1.Cells[3,row]))+2,(Length(SS)-Length(Deld((StringGrid1.Cells[3,row])))));
            endend;
      

  4.   

    我觉得你没有必要自定义一个函数,因为delphi中StringReplace这个函数就能满足你的要求。
    我模拟做了一下,不知是否满足你的要求.
    procedure TForm1.Button1Click(Sender: TObject);
    var
      row:Byte;
      SS,ss1:string;
    begin
      SS:=StringReplace(trim(edit1.text),' ','',[rfReplaceAll]);
      for row:=1 to  StringGrid1.RowCount+1 do
      begin
        ss1 := StringReplace(StringGrid1.Cells[3,row],'.','',[rfReplaceAll]);
        StringGrid1.Cells[4,row]:= Copy(SS,1,Length(ss1));
        SS:=Copy(SS,Length(ss1)+1,Length(SS)-Length(ss1));
      end
    end;procedure TForm1.Button2Click(Sender: TObject);
    var
      row: integer;
    begin
      for row:=1 to  StringGrid1.RowCount+1 do
      begin
        StringGrid1.Cells[1,row] := inttostr(row);
        StringGrid1.Cells[2,row] := 'II'+inttostr(row);
        if row = 1 then
        StringGrid1.Cells[3,row] := '11.22';
        if row = 2 then
        StringGrid1.Cells[3,row] := '33.44';
        if row = 3 then
        StringGrid1.Cells[3,row] := '55.66';
      end
    end;
      

  5.   


    function Deld(S:string):string;  
    var 
      i,j:Byte;
      ss:string; 
    begin
        ss:=''; 
        for j:=1 to length(s) do 
            begin 
          if copy(s,j,1) <>'.' then 
            begin 
            ss:=ss+copy(s,j,1); 
            end; 
        end; 
        Result:=ss; 
    end; 
      

  6.   

    谢谢楼上的各位 
    原来我没考虑空格,引起了错误
    StringReplace函数也很好用
      

  7.   

    另:
    StringGrid1.Cells[4,row]:= Copy(SS,1,(Length(Deld(StringGrid1.Cells[3,row]))+1));
      SS:=Copy(SS,Length(Deld(StringGrid1.Cells[4,row]))+2,(Length(SS)-Length(Deld((StringGrid1.Cells[4,row])))+2));