• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TStringBuilder类-Delphi

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

摘自万一老师的博客,略作整理。

//TStringBuilder.Create 可以无参数
procedure TForm1.Button1Click(Sender: TObject);
var
  sb: TStringBuilder;
begin
  sb := TStringBuilder.Create;
  sb.Append('Embarcadero');
  sb.Append(' CodeGear');
  sb.Append(' Delphi');
  sb.Append(' 2009');

  ShowMessage(sb.ToString); {Embarcadero CodeGear Delphi 2009}
  sb.Free;
end;

//TStringBuilder.Create 可用字符串初始化; 其他重载用处不大
procedure TForm1.Button2Click(Sender: TObject);
var
  sb: TStringBuilder;
begin
  sb := TStringBuilder.Create('Delphi ');
  sb.Append(2009);

  ShowMessage(sb.ToString); {Delphi 2009}
  sb.Free;
end;

//TStringBuilder.Append 可以添加几乎所有数据类型, 甚至是对象
procedure TForm1.Button1Click(Sender: TObject);
var
  sb1,sb2: TStringBuilder;
  ver: Cardinal;
begin
  sb1 := TStringBuilder.Create;
  sb2 := TStringBuilder.Create;

  sb1.Append('Delphi ');
  ver := 2009;
  sb1.Append(ver);
  ShowMessage(sb1.ToString); {Delphi 2009}

  sb2.Append(sb1);
  {换行}
  sb2.AppendLine;
  sb2.Append(Self);
  ShowMessage(sb2.ToString); {Delphi 2009
                              TForm1}

  sb1.Free;
  sb2.Free;
end;

//TStringBuilder.Append 截取字符数组和字符串
procedure TForm1.Button2Click(Sender: TObject);
const
  CharArr: array[0..6] of Char = ('A','B','C','D','E','F','G');
  Str = 'ABCDEFG';
var
  sb: TStringBuilder;
begin
  sb := TStringBuilder.Create;
  sb.Append(CharArr, 2, 3);
  ShowMessage(sb.ToString); {CDE}

  {清空}
  sb.Length := 0;

  sb.Append(Str, 2, 3);
  ShowMessage(sb.ToString); {CDE}

  sb.Free;
end;

//TStringBuilder.Append 添加指定数目的字符
procedure TForm1.Button3Click(Sender: TObject);
var
  sb: TStringBuilder;
begin
  sb := TStringBuilder.Create;
  sb.Append('A', 10);
  ShowMessage(sb.ToString); {AAAAAAAAAA}
  sb.Free;
end;

//TStringBuilder.Append 试试其他数据类型
procedure TForm1.Button4Click(Sender: TObject);
var
  sb: TStringBuilder;
  b: Boolean;
  r: Real;
begin
  sb := TStringBuilder.Create;

  b := True;
  sb.Append(b);
  sb.AppendLine;
  b := not b;
  sb.Append(b);
  sb.AppendLine;

  r := 100/3;
  sb.Append(r);

  ShowMessage(sb.ToString); {True
                             False
                             33.3333333333333}
  sb.Free;
end;

//TStringBuilder.AppendFormat 的使用和 Format 函数是一样的
procedure TForm1.Button5Click(Sender: TObject);
var
  sb: TStringBuilder;
begin
  sb := TStringBuilder.Create;
  sb.AppendFormat('%s %d', ['Delphi', 2009]);
  ShowMessage(sb.ToString); {Delphi 2009}
  sb.Free;
end;


//TStringBuilder.Replace 替换字符串
procedure TForm1.Button1Click(Sender: TObject);
var
  sb: TStringBuilder;
begin
  sb := TStringBuilder.Create;
  sb.Append('从 Delphi 2007 到 Delphi 2009');

  sb.Replace('Delphi', 'CBuilder');
  ShowMessage(sb.ToString);         {从 CBuilder 2007 到 CBuilder 2009}

  sb.Free;
end;

//TStringBuilder.Replace 替换字符
procedure TForm1.Button2Click(Sender: TObject);
var
  sb: TStringBuilder;
begin
  sb := TStringBuilder.Create;
  sb.Append('从 Delphi 2007 到 Delphi 2009');

  sb.Replace('0', '9');
  ShowMessage(sb.ToString);         {从 Delphi 2997 到 Delphi 2999}

  sb.Free;
end;

//TStringBuilder.Replace 在指定范围内替换字符
procedure TForm1.Button3Click(Sender: TObject);
var
  sb: TStringBuilder;
begin
  sb := TStringBuilder.Create;
  sb.Append('ABABABABABAB');

  sb.Replace('A', '-', 4, 5);
  ShowMessage(sb.ToString);    {ABAB-B-B-BAB}

  sb.Free;
end;

//TStringBuilder.Replace 在指定范围内替换字符串
procedure TForm1.Button4Click(Sender: TObject);
var
  sb: TStringBuilder;
begin
  sb := TStringBuilder.Create;
  sb.Append('ABC123ABC123ABC123');

  sb.Replace('ABC', '-', 3, 12);
  ShowMessage(sb.ToString);      {ABC123-123-123}

  sb.Free;
end;


//TStringBuilder 的插入与删除
procedure TForm1.Button1Click(Sender: TObject);
var
  sb: TStringBuilder;
begin
  sb := TStringBuilder.Create;
  sb.Append('CodeGear 2007');

  sb.Insert(Length('CodeGear '), 'Delphi ');
  ShowMessage(sb.ToString);      {CodeGear Delphi 2007}

  sb.Remove(Length('CodeGear Delphi'), Length(' 2007'));
  ShowMessage(sb.ToString);      {CodeGear Delphi}

  sb.Insert(sb.Length, ' 2009');
  ShowMessage(sb.ToString);      {CodeGear Delphi 2009}

  sb.Free;
end;


//TStringBuilder.CopyTo
procedure TForm1.Button1Click(Sender: TObject);
var
  sb: TStringBuilder;
  arr: TCharArray;
  i: Integer;
begin
  sb := TStringBuilder.Create;
  sb.Append('123456789');

  SetLength(arr, 9);
  for i := Low(arr) to High(arr) do arr[i] := '*';

  sb.CopyTo(3, arr, 1, 5);
  ShowMessage(PChar(arr));   {*45678***}

  sb.Free;
end;

//TStringBuilder.Chars
procedure TForm1.Button2Click(Sender: TObject);
var
  sb: TStringBuilder;
begin
  sb := TStringBuilder.Create('Delphi 2009');

  ShowMessage(sb.Chars[0]);             {D}
  ShowMessage(sb.Chars[sb.Length - 1]); {9}

  sb.Free;
end;


//TStringBuilder.ToString 可以带参数
procedure TForm1.Button1Click(Sender: TObject);
var
  sb: TStringBuilder;
begin
  sb := TStringBuilder.Create('123456789');

  ShowMessage(sb.ToString);       {123456789}
  ShowMessage(sb.ToString(3, 4)); {4567}

  sb.Free;
end;

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
DELPHIXEAndroid开发笔记发布时间:2022-07-18
下一篇:
delphi回调函数发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap