Everytime I look at some of the more expert code online I see things like %s
and %d
in some strings, especially in dialogs, but I have no idea what they are. I have googled the terms and I can't seem to find the answer and whether it is Delphi-bound or something common to every programming language.
I saw a post relating to C saying that it is used to "convert variables at runtime", how many arguments can we specify in a single string if this is the case?
Example usage:
ShowMessageFmt('Day %d = %s',[i,Days[i]]);
found at Delphi Basics.
Minimum Field Width Specifier In the above program, %8d specifier displays the value after 8 spaces while %-8d specifier will make a value left-aligned.
Description. The Format function provides 'C' like formatting of multiple of simple data types into a string. It provides very precise control over this formatting. The Formatting parameter defines how the Data array is manipulated into the returned string.
The %d operator is used as a placeholder to specify integer values, decimals, or numbers. It allows us to print numbers within strings or other values. The %d operator is put where the integer is to be specified. Floating-point numbers are converted automatically to decimal values.
"%5d" Format a string with the required number of integers and also pad with spaces to the left side if integers are not adequate. "%05d" Format a string with the required number of integers and also pad with zeroes to the left if integers are not adequate.
Those are format strings, similar to those used in C printf()
. They're also used by the Delphi Format
function, which again is similar to printf()
in C.
%d
represents an integer. It will be replaced by the content of the variable i
that is provided in the array that follows it.
%s
represents a string. It will be replaced by the content of Days[i]
that is passed in the array that follows it.
You can find more information in the Delphi documentation for SysUtils.Format
, specifically in the sub-section regarding Format Strings
.
These are format strings that are passed to the Format
function. Read all about it in the documentation.
Each placeholder in your format string is replaced by a value from the arguments open array. So, %d
is replaced with the value of i
, and %s
is replaced with the value of Days[i]
.
The format string placeholders specify the data type and formatting information. So, %d
is used to display an integer value in decimal representation, and %s
is used to indicate a string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With