有这样一个字符串s:="12345"   现在要求将它定长为:s:="12345     ",  即将它定长为10  个字符。
是不是用format()  ? 函数??  我试了好多次就是不行。

解决方案 »

  1.   

    j:=Length(s);
    for i:=1 to (10-j) do
      begin
        s:=s+' ';
      end;
      

  2.   

    copy('12345'+'          ',1,10)
      

  3.   

    function AddSpace(AString: string; ALength: integer): string;
    var
      i: integer;
    begin
      if Length(AString) < 10 then
      begin
        for i := Length(AString) to 9 do
          Result := AString + ' ';
      end
      else Result := AString;
    end;
      

  4.   

    再修改一下:
    function AddSpace(AString: string; ALength: integer): string;
    var
      i: integer;
    begin
      if Length(AString) < ALength then
      begin
        for i := Length(AString) to ALength - 1 do
          Result := AString + ' ';
      end
      else Result := AString;
    end;
    如果你最大长度为:10则带参数ALength为10;
      

  5.   

    又跟 jinjazz 学了一招!
      

  6.   

    还是 jinjazz(近身剪(N-P攻略)) 厉害些。
      

  7.   

    //虽然已结还是要说一下Format的用法
    var
      S: string;
    begin
      S := '12345';
      S := Format('%-10s', [S]);
    end;