String 字符串格式过程类似于writeln
使用String 字符串格式过程类似于writeln,string,file,delphi,format,String,File,Delphi,Format,使用writeln我可以将数字格式化为一行文本 var file: text; mystring: string; begin writeln(file,'Result is: ', var1:8:2,' | ', var2:8:2,' |'); end; Delphi中是否有一个类似的易于使用的程序可以处理类似的结果 _format_string(mystring, 'Result is: ', var1:8:2,' | ', var2:8:2,' |'); 谢谢。您可
writeln
我可以将数字格式化为一行文本
var
file: text;
mystring: string;
begin
writeln(file,'Result is: ', var1:8:2,' | ', var2:8:2,' |');
end;
Delphi中是否有一个类似的易于使用的程序可以处理类似的结果
_format_string(mystring, 'Result is: ', var1:8:2,' | ', var2:8:2,' |');
谢谢。您可以使用以下功能:
mystring := Format('The image dimensions are %d × %d.', [640, 480]);
从技术上讲,答案是“是”。但不推荐这样做
您可以根据类型编写自己的
我在过去(特别是在turbopascal时代)为了调试的目的做过这件事。这需要大量工作,需要您编写一个特殊的MyAssign
过程,并确保在try..finally
块中使用TTextRec
关闭Text
文件。笨重,但可行
一个更容易的选择是
使用格式
最酷的一点是,您不仅可以在格式字符串
中参数化宽度
和精度
,还可以像您通常提供值那样作为参数
您可以非常接近于使用8的宽度
和2的精度
,如您问题中的示例
例如,引用文档:
Format ('%*.*f', [8, 2, 123.456]);
相当于:
这是
格式
和格式字符串
的一个经常被忽视的功能,尽管Jeroen不推荐它,但我大约在一年前做过类似的事情——只是为了学习如何做。代码如下:
type
TTextFile = class
private type
TTextRecHelper = record helper for TTextRec
public
function GetTextFile: TTextFile;
procedure SetTextFile(const Value: TTextFile);
property TextFile: TTextFile read GetTextFile write SetTextFile;
end;
private var
FBuilder: TStringBuilder;
class function TextClose(var F: TTextRec): Integer; static;
class function TextIgnore(var F: TTextRec): Integer; static;
class function TextInput(var F: TTextRec): Integer; static;
class function TextOpen(var F: TTextRec): Integer; static;
class function TextOutput(var F: TTextRec): Integer; static;
procedure AppendString(const Value: string);
procedure AssignFile(var F: Text);
public
var F: Text;
constructor Create;
destructor Destroy; override;
function ToString: string; override;
end;
constructor TTextFile.Create;
begin
inherited Create;
FBuilder := TStringBuilder.Create();
AssignFile(F);
Rewrite(F);
end;
destructor TTextFile.Destroy;
begin
Close(F);
FBuilder.Free;
inherited Destroy;
end;
procedure TTextFile.AppendString(const Value: string);
begin
FBuilder.Append(Value);
end;
procedure TTextFile.AssignFile(var F: Text);
begin
FillChar(F, SizeOf(F), 0);
with TTextRec(F)do
begin
Mode := fmClosed;
BufSize := SizeOf(Buffer);
BufPtr := @Buffer;
OpenFunc := @TextOpen;
TextFile := Self;
end;
end;
class function TTextFile.TextClose(var F: TTextRec): Integer;
begin
Result := 0;
end;
class function TTextFile.TextIgnore(var F: TTextRec): Integer;
begin
Result := 0;
end;
class function TTextFile.TextInput(var F: TTextRec): Integer;
begin
F.BufPos := 0;
F.BufEnd := 0;
Result := 0;
end;
class function TTextFile.TextOpen(var F: TTextRec): Integer;
begin
if F.Mode = fmInput then
begin
F.InOutFunc := @TextInput;
F.FlushFunc := @TextIgnore;
F.CloseFunc := @TextIgnore;
end else
begin
F.Mode := fmOutput;
F.InOutFunc := @TextOutput;
F.FlushFunc := @TextOutput;
F.CloseFunc := @TextClose;
end;
Result := 0;
end;
class function TTextFile.TextOutput(var F: TTextRec): Integer;
var
AStr: AnsiString;
begin
SetLength(AStr, F.BufPos);
Move(F.BufPtr^, AStr[1], F.BufPos);
F.TextFile.AppendString(string(AStr));
F.BufPos := 0;
Result := 0;
end;
function TTextFile.ToString: string;
begin
Close(F);
result := FBuilder.ToString;
Rewrite(F);
end;
function TTextFile.TTextRecHelper.GetTextFile: TTextFile;
begin
Move(UserData[1], Result, Sizeof(Result));
end;
procedure TTextFile.TTextRecHelper.SetTextFile(const Value: TTextFile);
begin
Move(Value, UserData[1], Sizeof(Value));
end;
根据您的问题,如何使用它的示例:
tf := TTextFile.Create;
try
Writeln(tf.F, 'Result is: ', var1:8:2,' | ', var2:8:2,' |');
Caption := tf.ToString;
finally
tf.Free;
end;
关于Writeln的注释
输出:
Delphi似乎不尊重旧的Pascal格式。这就是为什么Writeln
output使用
,我下面的其他方法使用,
(我有一个Windows版本)
TStringBuilder
在现代的Delphi版本中,提供了一种优雅的字符串连接方式,并支持。它的格式化功能有限,但包含格式
风格(作为常规的格式
功能,非常有用,但缺少类型检查):
输出:
插入运算符
使用一些技巧,例如运算符重载和,可以模拟C++:
你的例子是:
Memo.Lines.Add(
stringout
< 'Result is: ' < soWidth(8) < soPrec(2) < var1 < ' | '
< soWidth(8) < soPrec(2) < var2 < ' |'
);
当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;
类型
PStringOut=^TStringOut;
TStringOutManipulatorRef=程序参考(pso:PStringOut);
pstringoutternalstorage=^tstringoutternalstorage;
tstringoutternalstorage=记录
数据:TStringBuilder;
宽度、精度:整数;
程序格式;内联;
函数GetFormatString(formatType:char):string;
结束;
IStringOutInternal=接口
存储功能:pstringoutineralstorage;
结束;
TStringOutInternal=类(TInterfacedObject,IStringOutInternal)
严格保密
储存:TStringOutInternalStorage;
私有的
构造函数创建;
存储功能:pstringoutineralstorage;
公众的
毁灭者毁灭;推翻
结束;
TStringOut=记录
私有的
缓冲区:IStringOutternal;
公众的
//插入运算符
类运算符LessThan(const this:TStringOut;add:string):TStringOut;
类运算符LessThan(const this:TStringOut;add:char):TStringOut;
类运算符LessThan(const this:TStringOut;add:integer):TStringOut;
类运算符LessThan(const this:TStringOut;add:double):TStringOut;
类运算符LessThan(const this:TStringOut;操纵器:TStringOutManipulatorRef):TStringOut;内联;
//隐式转换为字符串(“提取”运算符)
类运算符隐式(const this:TStringOut):字符串;内联;
结束;
{TStringOutInternalStorage}
程序TStringOutInternalStorage.ClearFormat;
开始
宽度:=0;
精度:=0;
结束;
函数tstringoutternalstorage.GetFormatString(formatType:char):字符串;
开始
结果:='%';
如果宽度>0,则
结果:=结果+IntToStr(宽度);
如果精度>0,则
结果:=结果+'.'+IntToStr(精度);
结果:=结果+格式类型;
结束;
{Tstringoutternal}
构造函数tstringoutternal.Create;
开始
继承;
Storage.Data:=TStringBuilder.Create;
结束;
析构函数Tstringoutternal.Destroy;
开始
存储。数据。免费;
继承;
结束;
函数tstringoutintternal.TheStorage:pstringoutintternalstorage;
开始
结果:=@存储;
结束;
{TStringOut}
类运算符TStringOut.Implicit(const this:TStringOut):字符串;
开始
结果:=this.Buffer.TheStorage.Data.ToString;
结束;
类运算符TStringOut.LessThan(const this:TStringOut;add:string):TStringOut;
开始
this.Buffer.TheStorage.Data.AppendFormat(this.Buffer.TheStorage.GetFormatString('s'),[add]);
this.Buffer.TheStorage.ClearFormat;
Result.Buffer:=此.Buffer;
结束;
类运算符TStringOut.LessThan(const this:TStringOut;add:char):TStringOut;
开始
this.Buffer.TheStorage.Data.Append(add);
this.Buffer.TheStorage.ClearFormat;
Result.Buffer:=此.Buffer;
结束;
类运算符TStringOut.LessThan(const this:TStringOut;add:integer):TStringOut;
开始
this.Buffer.TheStorage.Data.AppendFormat(this.Buffer.TheStorage.GetFormatString('d'),[add]);
this.Buffer.TheStorage.ClearFormat;
Result.Buffer:=此.Buffer;
结束;
类运算符TStringOut.LessThan(const this:TStringOut;add:double):TStringOut;
变量
s:pstringoutternalstorage;
开始
s:=this.Buffer.TheStorage;
如果s.精度为0
然后是s.Data.AppendFormat(s.GetFormatString('f'),[add])
else s s.Data.AppendFormat(s.GetFormatString('g'),[add]);
s、 清晰格式;
Result.Buffer:=此.Buffer;
结束;
类运算符TStringOut.LessThan(const this:TStringOut;操纵器:TStringOutManipulatorRef):TStringOut;
开始
结果:=这个;
操纵器(@Result);
结束;
{操纵器}
函数soEndl:TStringOutManipulatorRef;
开始
结果:=
Result is: 4.50 | 0.67 |
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 |
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 |
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;