博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
测试字符串写入类: TStringWriter
阅读量:6098 次
发布时间:2019-06-20

本文共 2845 字,大约阅读时间需要 9 分钟。

  hot3.png

以前没见过这个类, 它是在 TStringBuilder 的基础上实现的; 使用它可以方便地把各种数据类型写入字符串.

unit Unit1;interfaceuses  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{$R *.dfm}// 其 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、Charprocedure 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.

转载于:https://my.oschina.net/hermer/blog/320862

你可能感兴趣的文章
移动端架构的几点思考
查看>>
Tomcat与Spring中的事件机制详解
查看>>
Spark综合使用及用户行为案例区域内热门商品统计分析实战-Spark商业应用实战...
查看>>
初学者自学前端须知
查看>>
Retrofit 源码剖析-深入
查看>>
企业级负载平衡简介(转)
查看>>
ICCV2017 论文浏览记录
查看>>
科技巨头的交通争夺战
查看>>
当中兴安卓手机遇上农行音频通用K宝 -- 卡在“正在通讯”,一直加载中
查看>>
Shell基础之-正则表达式
查看>>
JavaScript异步之Generator、async、await
查看>>
讲讲吸顶效果与react-sticky
查看>>
c++面向对象的一些问题1 0
查看>>
直播视频流技术名词
查看>>
iOS13-适配夜间模式/深色外观(Dark Mode)
查看>>
网易跟贴这么火,背后的某个力量不可忽视
查看>>
企业级java springboot b2bc商城系统开源源码二次开发-hystrix参数详解(八)
查看>>
java B2B2C 多租户电子商城系统- 整合企业架构的技术点
查看>>
IOC —— AOP
查看>>
比特币现金将出新招,推动比特币现金使用
查看>>