//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;
end.