随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253


问题来源: http://www.cnblogs.com/del/archive/2007/12/18/1005161.html#1786227

{ 函数 }
function ToAlignString(const str: string; len: Integer): string;
var
  i,n: Integer;
  s: string;
begin
  if Abs(len) < Length(str) then Result := str;
  n := (Abs(len) - Length(str)) * SizeOf(Char);
  for i := 1 to Length(str) do if Ord(str[i]) < 256 then Inc(n);
  if n > 0 then s := StringOfChar(' ', n);
  if Len > 0 then Result := s + str else Result := str + s;
end;

{ 测试 }
procedure TForm1.FormCreate(Sender: TObject);
var
  s1,s2: string;
begin
  Memo1.Font.Name := '宋体';
  Memo1.Font.Size := 9;
  Memo1.Clear;

  s1 := '中国abc';
  s2 := '中国人民abc';
  Memo1.Lines.Add(ToAlignString(s1, -10));
  Memo1.Lines.Add(ToAlignString(s2, -10));
  Memo1.Lines.Add(ToAlignString(s1, 10));
  Memo1.Lines.Add(ToAlignString(s2, 10));
end;

posted on 2010-03-24 16:47  万一  阅读(...)  评论(...编辑  收藏