TStringBuilder (C++)
Description
This example exercises many of the members of the TStringBuilder class.
Code
template <class T>
void testAppend(T t)
{
	std::auto_ptr<TStringBuilder> builder(new TStringBuilder());
	builder->Append(t);
	UnicodeString s1 = builder->ToString();
	UnicodeString s2(t);
	assert(s1 == s2);
}
#if defined(_DELPHI_STRING_UNICODE)
const wchar_t* ext_chars =
	L"А Б В Г Д Є Ж Ѕ З И І"
	L"К Л М Н О П Ҁ Р С Т Ѹ"
	L"Ф Х Ѡ Ц Ч Ш Щ Ъ Ы Ь Ѣ"
	L"Ю ІА Ѧ Ѩ Ѫ Ѭ Ѯ Ѱ Ѳ Ѵ Ѥ";
#else
const char* ext_chars =
	"À Á Â Ã Ä Å Æ Ç È É Ê Ë Ì Í Î Ï"
	"Ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ü Ý Þ ß"
	"à á â ã ä å æ ç è é ê ë ì í î ï"
	"ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ";
#endif
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	{
		// Test starts out as empty.
		std::auto_ptr<TStringBuilder> builder(new TStringBuilder());
		assert(builder->ToString() == System::String());
	}
	{
		// Append.
		short s = 0xffe;
		testAppend(s);
		unsigned short us = 0xfffe;
		testAppend(us);
	}
	{
		const _DCHAR* p = ext_chars;
		std::auto_ptr<TStringBuilder> builder(new TStringBuilder());
		System::String str1(p);
		// Appending of wchar_t
		while (*p) 
		{
			builder->Append(*p++);
		}
		// Length property
		assert(builder->Length == str1.Length());
		// Subscript operator
		for (int i=0; i<str1.Length(); i++)
		{
			// Note: Strings are 1-based.
			assert((*(builder.get()))[i] == str1[i+1]);
		}
		// ToString()
		System::String str2 = builder->ToString();
		Edit1->Text = str1;
		Edit2->Text = str2;
		assert(str2 == str1);
		// Replace.
		builder->Replace(' ', '.');
		UnicodeString str3;
		str3 = builder->ToString();
		Edit3->Text = str3;
		assert(str3 != str2);
		// Replace back.
		builder->Replace(L'.', L' ');
		System::String str4 = builder->ToString();
		Edit4->Text = str4;
		assert(str2 == str4);
	}
	{
		System::String str(ext_chars);
		std::auto_ptr<TStringBuilder> builder(new TStringBuilder());
		// Insert at start when empty, at start when not empty, and at end.
		builder->Insert(0, str);
		builder->Insert(0, str);
		builder->Insert(builder->Length, str);
		// Concatenate the string three times.
		System::String str2;
		str2 += str;
		str2 += str;
		str2 += str;
		str = builder->ToString();
		Edit5->Text = str;
		assert(builder->ToString() == str2);
	}
	MessageDlg("All assertions passed in the TStringBuilder Test test.",
		mtInformation, TMsgDlgButtons() << mbOK, 0);
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
	const _DCHAR* p = ext_chars;
	std::auto_ptr<TStringBuilder> builder(new TStringBuilder());
	// Appending of chars
	while (*p) 
	{
		builder->Append(*p++);
	}
	System::String str1(ext_chars);
	System::String str2 = builder->ToString();
	assert(str1 == str2);
	builder->Replace(_D(" "), _D("."));
	System::String str3;
	str3 = builder->ToString();
	assert(str1 != str3);
	builder->Replace(_D("."), _D(" "));
	System::String str4(builder->ToString());
	assert(str1 == str4);
	MessageDlg("All assertions passed in the TStringBuilder Replace test.",
		mtInformation, TMsgDlgButtons() << mbOK, 0);
}
Uses
- System.SysUtils.TStringBuilder ( fr | de | ja )