有一个字符串长度不够10位,只有5位,我要在该字符串后面加空格补齐,该如何实现,有没有函数可以实现?
如:'AAAAA'要转换成'AAAAA     '

解决方案 »

  1.   

    使用方法: fillright(inputstr,'A',10);//右填充
    function fillRight( str :string; c :char;len : integer):string;stdcall;
      var i,lengs :integer;
          tmp:string;
    begin
      lengs :=length(str);
      tmp:=str;
      //大于等于,直接返回
      if (lengs >= len) then
         begin
           result :=tmp;
         end
      else
         begin
           //从右侧填充
           for i:=1 to (len-lengs) do
             begin
               tmp:=tmp+c;
             end;
           result:=tmp;
         end;
    end;
      

  2.   


    function FillRight(S: String;  L: Integer):String;
    begin
      Result := S + StringOfChar(' ', L - Length(S));
    end;
    一个调用的例子:
      ShowMessage(FillRight('AAAAA', 10));
      

  3.   

    用Format很方便。
    var
      S: string;
    begin
      S := 'AAAAA';
      Writeln(Format('"%10s"', [S])); // "     AAAAA"
      Writeln(Format('"%-10s"', [S])); // "AAAAA     "
    end;