Wednesday, March 2, 2011

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.

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Kang Iwan K-sev | Thank's for your visit To My Site - Ridwan Mulyana | Cibeureum