(page requirements)
Downloads

Using Format() to format your code

When you have to construct long strings combining different types of data using many functions in between, it maybe better and easier to use the "Format()" functions instead of using "+" signs.
 
For example, consider the following simple string (assuming that "sName" is a string variable containing a name):
 
'My name is ' + sName + ' and I am '
              + IntToStr( 16 )
              + ' years old'
Listing #1 : Delphi code. Download strcc (0.19 KB).
 
It's better to use "Format()" to achieve the same result, because it makes it easier to read and format your source code:
 
Format(
  'My name is %s and I am %d years old',
  [ sName, 16 ] )
Listing #2 : Delphi code. Download strcc2 (0.19 KB).
 
Of course, "Format()" can handle many other types of data and format them in many ways, so be sure to lookup help for it.
 
 
Applicable Keywords : Delphi, Delphi 2.x, Delphi 3.x, Functions
 
 
 
Copyright © 2009 Chami.com. All Rights Reserved. | Advertise | Created in HTML Kit editor