关于 Writeln 的注意事项
Writeln(file, 'Result is: ', var1:8:2,' | ', var2:8:2,' |');
输出:
Result is: 4.50 | 0.67 |
似乎 Delphi 在不尊重DecimalSeparator的情况下执行旧的 Pascal 格式。这就是为什么Writeln
使用输出.
和我下面使用的其他方法的原因,
(我有一个西班牙语版本的 Windows)。
TStringBuilder
在现代 Delphi 版本中,TStringBuilder提供了一种优雅的字符串连接方式,支持流畅的接口。它的格式化功能有限,但包含一种Format
风格(作为常规Format
函数,它非常有用,但缺少类型检查):
sb := TStringBuilder.Create;
try
sb.Append('Result is: ').Append(var1).Append(' | ').Append(var2).Append(' |');
Memo.Lines.Add(sb.ToString);
sb.Clear;
sb.AppendFormat('Result is: %8.2f | %8.2f |', [var1, var2]);
Memo.Lines.Add(sb.ToString);
finally
sb.Free;
end;
输出:
Result is: 4,5 | 0,666666666666667 |
Result is: 4,50 | 0,67 |
插入运算符
使用一些技巧,例如运算符重载和闭包,可以模仿C++ ostream 插入运算符:
Memo.Lines.Add(stringout < 'My ' < 5 < ' cents' < soEndl < '2/3: ' < soPrec(4) < 2/3);
输出:
My 5 cents
2/3: 0,6667
你的例子:
Memo.Lines.Add(
stringout
< 'Result is: ' < soWidth(8) < soPrec(2) < var1 < ' | '
< soWidth(8) < soPrec(2) < var2 < ' |'
);
输出:
Result is: 4,50 | 0,67 |
当 Delphi 在类中支持运算符重载时,实现会更加简洁。同时,使用记录进行运算符重载和接口进行自动内存管理可以解决问题:
type
PStringOut = ^TStringOut;
TStringOutManipulatorRef = reference to procedure(pso: PStringOut);
PStringOutInternalStorage = ^TStringOutInternalStorage;
TStringOutInternalStorage = record
Data: TStringBuilder;
Width, Precision: integer;
procedure ClearFormat; inline;
function GetFormatString(formatType: char): string;
end;
IStringOutInternal = interface
function TheStorage: PStringOutInternalStorage;
end;
TStringOutInternal = class(TInterfacedObject, IStringOutInternal)
strict private
Storage: TStringOutInternalStorage;
private
constructor Create;
function TheStorage: PStringOutInternalStorage;
public
destructor Destroy; override;
end;
TStringOut = record
private
Buffer: IStringOutInternal;
public
// insertion operator
class operator LessThan(const this: TStringOut; add: string): TStringOut;
class operator LessThan(const this: TStringOut; add: char): TStringOut;
class operator LessThan(const this: TStringOut; add: integer): TStringOut;
class operator LessThan(const this: TStringOut; add: double): TStringOut;
class operator LessThan(const this: TStringOut; manipulator: TStringOutManipulatorRef): TStringOut; inline;
// implicit conversion to string ("extraction" operator)
class operator Implicit(const this: TStringOut): string; inline;
end;
{ TStringOutInternalStorage }
procedure TStringOutInternalStorage.ClearFormat;
begin
Width := 0;
Precision := 0;
end;
function TStringOutInternalStorage.GetFormatString(formatType: char): string;
begin
Result := '%';
if Width > 0 then
Result := Result + IntToStr(Width);
if Precision > 0 then
Result := Result + '.' + IntToStr(Precision);
Result := Result + formatType;
end;
{ TStringOutInternal }
constructor TStringOutInternal.Create;
begin
inherited;
Storage.Data := TStringBuilder.Create;
end;
destructor TStringOutInternal.Destroy;
begin
Storage.Data.Free;
inherited;
end;
function TStringOutInternal.TheStorage: PStringOutInternalStorage;
begin
Result := @Storage;
end;
{ TStringOut }
class operator TStringOut.Implicit(const this: TStringOut): string;
begin
Result := this.Buffer.TheStorage.Data.ToString;
end;
class operator TStringOut.LessThan(const this: TStringOut; add: string): TStringOut;
begin
this.Buffer.TheStorage.Data.AppendFormat(this.Buffer.TheStorage.GetFormatString('s'), [add]);
this.Buffer.TheStorage.ClearFormat;
Result.Buffer := this.Buffer;
end;
class operator TStringOut.LessThan(const this: TStringOut; add: char): TStringOut;
begin
this.Buffer.TheStorage.Data.Append(add);
this.Buffer.TheStorage.ClearFormat;
Result.Buffer := this.Buffer;
end;
class operator TStringOut.LessThan(const this: TStringOut; add: integer): TStringOut;
begin
this.Buffer.TheStorage.Data.AppendFormat(this.Buffer.TheStorage.GetFormatString('d'), [add]);
this.Buffer.TheStorage.ClearFormat;
Result.Buffer := this.Buffer;
end;
class operator TStringOut.LessThan(const this: TStringOut; add: double): TStringOut;
var
s: PStringOutInternalStorage;
begin
s := this.Buffer.TheStorage;
if s.Precision <> 0
then s.Data.AppendFormat(s.GetFormatString('f'), [add])
else s.Data.AppendFormat(s.GetFormatString('g'), [add]);
s.ClearFormat;
Result.Buffer := this.Buffer;
end;
class operator TStringOut.LessThan(const this: TStringOut; manipulator: TStringOutManipulatorRef): TStringOut;
begin
Result := this;
manipulator(@Result);
end;
{ Manipulators }
function soEndl: TStringOutManipulatorRef;
begin
Result :=
procedure(pso: PStringOut)
begin
pso.Buffer.TheStorage.Data.AppendLine;
pso.Buffer.TheStorage.ClearFormat;
end;
end;
function soWidth(value: integer): TStringOutManipulatorRef;
begin
Result :=
procedure(pso: PStringOut)
begin
pso.Buffer.TheStorage.Width := value;
end;
end;
function soPrec(value: integer): TStringOutManipulatorRef;
begin
Result :=
procedure(pso: PStringOut)
begin
pso.Buffer.TheStorage.Precision := value;
end;
end;
{ The stringout "constructor" }
function stringout: TStringOut; inline;
begin
Result.Buffer := TStringOutInternal.Create;
end;