TStringBuilder类 - Delphi
Posted 情怀丶感伤
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TStringBuilder类 - Delphi相关的知识,希望对你有一定的参考价值。
摘自万一老师的博客,略作整理。
//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;
以上是关于TStringBuilder类 - Delphi的主要内容,如果未能解决你的问题,请参考以下文章
Delphi 的TStringBuilder防止服务器内存碎片化
登录窗口不是系统主窗口 但又需要最先显示 用delphi怎么编写代
转:Java JNI 和 Delphi 相互調用-JAVA調用DELPHI