Strings¶
What is a String?¶
An unqualified String does not have one fixed underlying type. Its meaning
depends on the compiler mode and string-related directives:
- With
{$H-},Stringis an alias forShortString, whose payload is limited to 255 bytes. - With
{$H+}and Unicode strings disabled,Stringis an alias for the variable-lengthAnsiString. - With
{$mode delphiunicode}or{$modeswitch unicodestrings},Stringis an alias forUnicodeString, which uses UTF-16.
An unqualified AnsiString uses the dynamic code page CP_ACP. Free Pascal
uses the program's DefaultSystemCodePage when conversions involving that code
page are required. Do not assume that every String has the same encoding on
every platform; use an explicitly encoded string type and convert at system
boundaries when the encoding matters.
Refs:
- DelphiUnicode compiler mode
- UnicodeStrings mode switch
- What is a
String? - https://forum.lazarus.freepascal.org/index.php?topic=58131.0
Display UTF-8 on a console¶
Alternatively, you can assign your UTF-8 test to a string variable.
Note
If you see garbage characters on console;
- your console might not support code page 65001, or
- your windows does not support UTF on API level (only read/write file in UTF-8)
See this answer from StackOverflow on how to enable code page 65001 on your console.
Warning
The same answer from StackOverflow also shows how to enable UTF-8 on Windows (system-wide).
DO NOT MISS the caveat section and comments in from that answer.
Enabling UTF-8 system-wide on Windows is currently in beta and could lead to unintended system-wide side effects.
Refs:
- https://wiki.freepascal.org/FPC_Unicode_support#Code_pages
- https://stackoverflow.com/a/57134096
- https://superuser.com/a/1435645
What is my program's default code page?¶
DefaultSystemCodePage is the code page that Free Pascal uses when it encounters
CP_ACP. It is not a reliable way to query the operating system's actual code
page because a program can change it.
On Windows, Free Pascal initializes it from the Windows ANSI code page. On Unix,
cwstring initializes Free Pascal's C-library-based string conversion support
from the process locale. It uses locale settings derived from LC_ALL,
LC_CTYPE, or LANG and sets DefaultSystemCodePage. Free Pascal recommends
placing cwstring among the first units in the program's uses clause:
A value of 65001 means UTF-8. A value of 0 is the CP_ACP identifier; it
means "use the program's default ANSI code page" rather than "the system uses
ASCII". The console's input/output encoding and font support are separate, so a
UTF-8 default code page does not by itself guarantee that every character will
display correctly.
See the Free Pascal documentation for
DefaultSystemCodePage
and CP_ACP.
Remove trailing chars at the end of a string¶
Contribution
Gustavo 'Gus' Carreno, from the Unofficial Free Pascal Discord server, shared a neat trick to remove trailing characters by using SetLength(str, length(str) - n);.
Let's say you have a loop that append strings with trailing characters at the end.
One way to remove trailing characters is use a flag to inside the for loop. The logic would be: do not add commas or spaces if we are at the end of the loop.
A simpler way is to use SetLength(str, length(str) - n_chars_to_remove);.
See the example below.
- The
forloop completes a sentence with a comma and a space at the end. Line 19-20. - The trick;
SetLength(line, length(line) - 2);removes the last 2 chars from the end of the sentence. Line 29.