WriteStr: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: WriteStr
| Old | New | Line | |
|---|---|---|---|
| 1 | + | The [[Procedure|procedure]] [`writeStr`](https://www.freepascal.org/docs-html/rtl/system/writestr.html) formats arguments using the same syntax as [[Write|write/writeLn]], but the destination is a [[String|string]] variable. It is an [[Extended Pascal|extended Pascal]] extension, but the [[FPC]]’s default [[RTL]] supports this procedure regardless of the current [[Compiler Mode|compiler compatibility mode]], not just [[Mode extendedpascal|{$mode extendedPascal}]]. | |
| 2 | + | ||
| 3 | + | ## signature | |
| 4 | + | ||
| 5 | + | `WriteStr` needs at least two arguments. The first parameter has to be a string-assignment-compatible [[Variable|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`. | |
| 6 | + | ||
| 7 | + | ## behavior | |
| 8 | + | ||
| 9 | + | `WriteStr` behaves like `write`/`writeLn`, but does not need to open an external file for processing. | |
| 10 | + | ||
| 11 | + | 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. | |
| 12 | + | ||
| 13 | + | > **Note:** As an extension, the FPC allows referencing the destination variable as a source variable. The extended Pascal states “It shall be an error if any of the write-parameters accesses the referenced string-variable.” The FPC ignores this requirement and does not yield an error. | |
| 14 | + | ||
| 15 | + | ## application | |
| 16 | + | ||
| 17 | + | `WriteStr` is the go-to method of standard, yet simple formatting tasks. A few examples are: | |
| 18 | + | ||
| 19 | + | `writeStr(myString, someInteger:1);` is equivalent to `myString := intToStr(someInteger);` (or `myString := someInteger.toString();`), but the latter requires the [[sysutils|sysUtils unit]]. Note, the minimum width specifier `:1` is optional in non-ISO-compliant compiler modes. | |
| 20 | + | ||
| 21 | + | 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 [[Ansistring|ANSI string]]), but [`padLeft`](https://www.freepascal.org/docs-html/rtl/strutils/padleft.html) requires the `strUtils` unit.\ | |
| 22 | + | For reference, in ISO-compliant modes `writeStr(tableCell, caption:10);` is equivalent to the more complicated `tableCell := caption.subString(0, 10).padLeft(10);`. | |
| 23 | + | ||
| 24 | + | ## see also | |
| 25 | + | ||
| 26 | + | - [[ReadStr|readStr]] performs the reverse operation | |
| 27 | + | - [[Str|str]], similar to `writeStr` but less versatile (only *one* numeric or ordinal type argument permitted) | |
| 28 | + | ||
| 29 | + | --- | |
| 30 | + | *From the Free Pascal wiki, page "WriteStr", as it stood on 2021-01-24 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 31 | + | Category: FPC | |
| 32 | + | Category: Pascal |
