Building Strings

We have seen that the advent of multiple string types causes potential pitfalls in string concatenation (see section "Assigning and Converting Strings" in Chapter 2 and the section "String Operations That Fail or Slow Down" in Chapter 3). Combining these issues with the desire to have unified Win32 and .NET code whenever possible, it is no surprise that CodeGear took an idea from .NET and added it to the native RTL. The idea being a specific class to create a string by adding multiple elements of various data types. Called StringBuilder in .NET, the class has appropriately been renamed TStringBuilder in Delphi.

As a simple example of the use of the TStringBuilder class, consider the following code snippet (taken from the StringBuilder project) var sBuilder: TStringBuilder; str1: string; begi n sBuilder := TStringBuilder.Create; try sBui1der.Append(12); sBui1der.Append( 'hello'); str1 := sBuilder.ToString; fi nal ly sBuilder.Free; end; end;

Notice in the code the use of a try-finally block, as unlike a reference-counted string you have to remember to dispose the TStringBuilder object. Another element you can notice above is that there are many different data types that you can pass as parameters to the Append function. A complete list will show up in the editor thanks to Code Completion, as you can see in the following image:

Value: Boolean const Value: strings Startlndex: Integer; Count: Integer const Value: TCharArray; Startlndex: Integer; CharCount: Integer

Value: Char; RepeatCount: Integer

Value: Cardinal

Value: Word const Value: TCharArray

Value: Ulnt64

const Value: string

Value: Single

Value: Shortlnt

Value: TObject

Value: Int&4

Value: Integer

Value: Smalllnt

Value: Double

Value: Currency

Value: Char

Value: Byte

Other interesting methods of the TStringBuilder class include an AppendFormat (with an internal call to Format) and an AppendLine that adds the sLineBreak value. Along with Append, there is a corresponding series of Insert overloaded methods, as well as a Remove and a few Replace methods.

When you are done, you can use ToString to fetch the result of the various operations, but also check the Length or access to individual Chars by index. Notice, though that the semantics of the Chars method is different from that of the string[] operation. The former uses a 0-based index while the standard Delphi code uses a 1-based index. As a test, consider this method (part of the StringBuilder demo):

procedure TFormSBuilder.btnCharPosClick(Sender: TObject); var str1: string; sBuilder: TStringBuilder; begi n str1 := '1234567890';

sBuilder := TStringBuilder.Create (str1);

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment