Difference between revisions of "WriteStr"
(create requested article) |
(→application: cf. discussion) |
||
(One intermediate revision by the same user not shown) | |||
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>, but the latter requires the [[sysutils|<syntaxhighlight lang="pascal" inline>sysUtils</syntaxhighlight> unit]]. | + | <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. | ||
− | In non-ISO-compliant modes <syntaxhighlight lang="pascal" inline>writeStr(tableCell, finding:24);</syntaxhighlight> is equivalent to <syntaxhighlight lang="pascal" inline>tableCell := padLeft(finding, 24);</syntaxhighlight>, but {{Doc|package=RTL|unit=strutils|identifier=padleft|text=<syntaxhighlight lang="pascal" inline>padLeft</syntaxhighlight>}} requires the <syntaxhighlight lang="pascal" inline>strUtils</syntaxhighlight> unit. | + | In non-ISO-compliant modes <syntaxhighlight lang="pascal" inline>writeStr(tableCell, finding:24);</syntaxhighlight> is equivalent to <syntaxhighlight lang="pascal" inline>tableCell := padLeft(finding, 24);</syntaxhighlight> (or <syntaxhighlight lang="pascal" inline>tableCell := finding.padLeft(24);</syntaxhighlight> if <syntaxhighlight lang="pascal" inline>finding</syntaxhighlight> is an [[Ansistring|ANSI string]]), but {{Doc|package=RTL|unit=strutils|identifier=padleft|text=<syntaxhighlight lang="pascal" inline>padLeft</syntaxhighlight>}} requires the <syntaxhighlight lang="pascal" inline>strUtils</syntaxhighlight> unit. |
− | For reference, in ISO-compliant modes <syntaxhighlight lang="pascal" inline>writeStr(tableCell, caption:10);</syntaxhighlight> is equivalent to the more complicated <syntaxhighlight lang="pascal" inline>tableCell := | + | <br/><small>For reference, in ISO-compliant modes <syntaxhighlight lang="pascal" inline>writeStr(tableCell, caption:10);</syntaxhighlight> is equivalent to the more complicated <syntaxhighlight lang="pascal" inline>tableCell := caption.subString(0, 10).padLeft(10);</syntaxhighlight>.</small> |
== see also == | == see also == |
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);
.