測試字串寫入類 TStringWriter

2021-09-05 21:47:40 字數 3586 閱讀 4946

以前沒見過這個類, 它是在 tstringbuilder 的基礎上實現的; 使用它可以方便地把各種資料型別寫入字串.

unit unit1;

inte***ce

uses

windows, messages, sysutils, variants, classes, graphics, controls, forms,

dialogs, stdctrls;

type

tform1 = class(tform)

button1: tbutton;

button2: tbutton;

button3: tbutton;

button4: tbutton;

button5: tbutton;

button6: tbutton;

button7: tbutton;

procedure button1click(sender: tobject);

procedure button2click(sender: tobject);

procedure button3click(sender: tobject);

procedure button4click(sender: tobject);

procedure button5click(sender: tobject);

procedure button6click(sender: tobject);

procedure button7click(sender: tobject);

end;

var form1: tform1;

implementation

// 其 writeline、write 各有寫入多種資料型別的過載

procedure tform1.button1click(sender: tobject);

var writer: tstringwriter;

begin

writer := tstringwriter.create;

writer.writeline('aaa');

writer.writeline('bbb');

showmessage(writer.tostring);

writer.free;

writer := tstringwriter.create;

writer.write('aaa');

writer.write('bbb');

writer.writeline;

writer.write('ccc');

showmessage(writer.tostring);

writer.free;

end;

//寫入 boolean、char

procedure tform1.button2click(sender: tobject);

const

b1: boolean = true;

b2: boolean = false;

c1: char = 'a';

c2: char = 'b';

var writer: tstringwriter;

begin

writer := tstringwriter.create;

writer.writeline(b1);

writer.writeline(b2);

writer.writeline(c1);

writer.writeline(c2);

showmessage(writer.tostring);

writer.free;

end;

//寫入整數和浮點數

procedure tform1.button3click(sender: tobject);

const

n1 = 1;

n2 = -1;

f = 3.14;

var writer: tstringwriter;

begin

writer := tstringwriter.create;

writer.writeline(n1);

writer.writeline(n2);

writer.writeline(f);

showmessage(writer.tostring);

writer.free;

end;

//格式化寫入

procedure tform1.button4click(sender: tobject);

var writer: tstringwriter;

begin

writer := tstringwriter.create;

writer.write('%d + %d = %d', [1,2,3]);

showmessage(writer.tostring);

writer.free;

end;

//寫入字元陣列

procedure tform1.button5click(sender: tobject);

var writer: tstringwriter;

cs: tchararray;

i: integer;

begin

setlength(cs, 26);

for i := 0 to length(cs) - 1 do cs[i] := chr(65+i);

writer := tstringwriter.create;

writer.writeline(cs);

writer.writeline(cs, 1, 3);

showmessage(writer.tostring);

writer.free;

end;

//寫入物件(類名)

procedure tform1.button6click(sender: tobject);

var writer: tstringwriter;

begin

writer := tstringwriter.create;

writer.writeline(self);

writer.writeline(sender);

showmessage(writer.tostring);

writer.free;

end;

//從 tstringbuilder 建立

procedure tform1.button7click(sender: tobject);

var writer: tstringwriter;

sb: tstringbuilder;

begin

sb := tstringbuilder.create('abc');

writer := tstringwriter.create(sb);

writer.writeline;

writer.write(123);

showmessage(writer.tostring);

sb.free;

writer.free;

end;

end.

C 寫入字串

c 中幾個輸入函式的用法和區別 cin cin.get cin.getline getline gets getchar 1 cin 用法1 最基本,也是最常用的用法,輸入乙個數字 用法2 接受乙個字串,遇 空格 tab 回車 都結束 include using namespace std main...

scanf寫入字串給指標

include stdio.h main a 3 int i for i 0 i 3 i scanf d s a i nu,a i name for i 0 i 3 i printf 3d s n a i nu,a i name c語言是比較低階的語言,沒有字串概念,只有字元陣列,不能自動分配空間,...

字串寫入到json檔案

背景 php產生公告 傳送到cgi 在cgi把該公告的json 字串寫入到檔案內 轉義後的字串 通過 jsoncpp 操作 int write notice to json string str path,const string str content std string json file w...