Appending and Concatenating Strings

Another type of coding you should replace whenever possible is the string concatenation code. It is not too hard to look for occurrences of AppendStr in your code (also because using it causes a deprecated warning), but it is way more complicated to replace the direct string concatenation performed with the + sign.

Also, the suggestion is to replace direct string concatenation with the use of the TStringBuilder class in case you are performing a complex concatenation56. For showing a message made of two strings, keeping the + sign is certainly fine.

Again, what you should really look out for is concatenation that causes implicit string conversions, as this means copying the data around many, many times. In other words, appending and concatenating strings works fine when all the string variables (left-value and right-values) are of the same type (all UnicodeString, all AnsiString, all UTF8String, etc.). In that case, the compiler uses the correct code page for string literals.

56 Differently from what has been written, using the TStri ngBuilder class doesn't specifically improve performance, but makes the code more clear (in case of complex conversions and concatenations) and, if you need that, compatible with the .NET counterpart. The TStringBuilder class is covered in detail in Chapter 7.

When mixing strings using different code pages, explicit casts should be used to avoid hidden conversion that slow down the code (or cause data loss).

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