Difference between revisions of "WriteStr"
(→application: add type helpers) |
(→application: cf. discussion) |
||
Line 18: | Line 18: | ||
A few examples are: | A few examples are: | ||
− | <syntaxhighlight lang="pascal" inline>writeStr(myString, someInteger:1);</syntaxhighlight> is equivalent to <syntaxhighlight lang="pascal" inline>myString := intToStr(someInteger);</syntaxhighlight> (or <syntaxhighlight lang="pascal" inline>myString := someInteger.toString();</syntaxhighlight> | + | <syntaxhighlight lang="pascal" inline>writeStr(myString, someInteger:1);</syntaxhighlight> is equivalent to <syntaxhighlight lang="pascal" inline>myString := intToStr(someInteger);</syntaxhighlight> (or <syntaxhighlight lang="pascal" inline>myString := someInteger.toString();</syntaxhighlight>), but the latter requires the [[sysutils|<syntaxhighlight lang="pascal" inline>sysUtils</syntaxhighlight> unit]]. |
Note, the minimum width specifier <syntaxhighlight lang="pascal" inline>:1</syntaxhighlight> is optional in non-ISO-compliant compiler modes. | Note, the minimum width specifier <syntaxhighlight lang="pascal" inline>:1</syntaxhighlight> is optional in non-ISO-compliant compiler modes. | ||
Latest revision as of 16:19, 24 January 2021
The procedure writeStr
formats arguments using the same syntax as write
/writeLn
, but the destination is a string variable.
It is an extended Pascal extension, but the FPC’s default RTL supports this procedure regardless of the current compiler compatibility mode, not just {$mode extendedPascal}
.
signature
WriteStr
needs at least two arguments.
The first parameter has to be a string-assignment-compatible variable, or, if allowed, a proper “writable constant”.
The second and following arguments have the same type requirements and format specification syntax as for write
/writeLn
.
behavior
WriteStr
behaves like write
/writeLn
, but does not need to open an external file for processing.
If the destination parameter, the first argument, has a smaller capacity than the concatenated formatted strings, the result is clipped to the left, i. e. only the first x characters will be stored.
application
WriteStr
is the go-to method of standard, yet simple formatting tasks.
A few examples are:
writeStr(myString, someInteger:1);
is equivalent to myString := intToStr(someInteger);
(or myString := someInteger.toString();
), but the latter requires the sysUtils
unit.
Note, the minimum width specifier :1
is optional in non-ISO-compliant compiler modes.
In non-ISO-compliant modes writeStr(tableCell, finding:24);
is equivalent to tableCell := padLeft(finding, 24);
(or tableCell := finding.padLeft(24);
if finding
is an ANSI string), but padLeft
requires the strUtils
unit.
For reference, in ISO-compliant modes writeStr(tableCell, caption:10);
is equivalent to the more complicated tableCell := caption.subString(0, 10).padLeft(10);
.