Sign in Join
Wiki copytemporary

String: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

Title: String string

OldNewLine
1-**String** is a [[Type|type]] which may contain [[Character and string types|characters]].
2-
3-## Usage
4-
5-``` pascal
6-var
7- s, str1, str2, str3, str4: string;
8- c: char;
9- n: integer;
10-
11-str1 := 'abc'; // assignment
12-str2 := '123'; // string containing chars 1, 2 and 3
13-str3 := #13#10; // cr lf
14-str4 := 'this is a ''quoted'' string'; // use of quotes within a string
15-s := str1 + str2; // concatenation
16-c := s[1]; // use as index in array
17-n := Length( s ); // length of string s
18-```
19-
20-## Alias
21-
22-String is an alias for [[Character and string types|ShortString]], [[Character and string types|AnsiString]] or [[Character and string types|Unicodestring (UTF16)]] depending on a compiler setting.
23-
24-If [[Compiler|compiler]] directive {\$H} or [compiler directive](http://freepascal.org/docs-html/current/prog/progch1.html#x5-40001) {\$LongStrings} has been used with an "on" parameter ( {\$H+} or {\$LongStrings ON} ), then a String type is the same as an AnsiString type, if not ( {\$H-} or {\$LongStrings OFF} ), it is a ShortString type. What String is an alias for can also be set by the -Sh [command line option](http://www.freepascal.org/docs-html/user/userap1.html). FPC also supports {\$mode delphiunicode} for Delphi compatible UTF16 support.
25-
26-NOTE: The {\$mode} compiler directive will also set the String alias. After the compiler mode is set to FPC (the default), ObjFPC, MacPAS or TP, String will be an alias for ShortString. After the compiler mode is set to Delphi, String will be an alias for AnsiString. So the String alias setting should be made following the compiler mode setting to prevent it from being overridden:
27-
28-``` pascal
29-{$H+} // String is an alias for AnsiString
30-{$mode ObjFPC} // also affects String alias - String is now an alias for ShortString
31-{$H+} // String is now an alias for AnsiString
32-```
33-
34-A String variable declared with a length specifier will always be a ShortString regardless of the compiler setting for String alias.
35-
36-``` pascal
37-{$H+} // String is an alias for AnsiString
38-var
39- name : String[25]; // name is a ShortString variable since a length specification overrides the alias setting
40-```
41-
42-Note that all types of longstring are managed types, whereas ShortStrings are not managed types: they have no reference count.
43-
44-## String types
45-
46-The different string types - ShortString, AnsiString, WideString and UnicodeString - differ with respect to *length* and *content*:
47-
48-- [[Character and string types|ShortString]] has a *fixed maximum length* that is decided by the programmer (e.g. *name : String\[25\];*) but is limited to 255 characters. If a ShortString length is not explicitly given, then the length is implicitly set to 255. It is not reference counted.
49-- [[Character and string types|AnsiString]] has a *variable length* that is limited only by the value of High(SizeInt) (which is platfom dependant) and available memory. It is a reference counted type.
50-- RawByteString is an alias for AnsiString.
51-- [[Character and string types|WideString]] has a variable length like AnsiString but contains [[WideChar]] instead of [[Char]]. It is a BWSTR compatible string type and has no reference count.
52-- [[Character and string types|UnicodeString]] is similar to [[Character and string types|WideString]] but UnicodeString is a managed type and has a reference count whereas widestring is a BWSTR compatible stringtype that is COM compatible and is not reference counted.
53-
54-Note that BWSTR types rely on COM marshaling or - when used alone - copy semantics instead of reference counting. In a COM context they are governed by the COM marshaling subsystem if available. (i.e. Windows)
55-
56-[Note from compiler developer **PascalDragon**](https://forum.lazarus.freepascal.org/index.php/topic,61081.msg458774.html#msg458774): The WideString type on Windows is considered as managed. Also the WideString type on non-Windows is internally a UnicodeString. You can see that by looking at the used RTL helper function: e.g. `u := 'Foobar'` will use `fpc_unicodestr_assign` on non-Windows instead of `fpc_widestr_assign`.
57-
58-## String type in Lazarus
59-
60-The Lazarus IDE stores everything in UTF-8 encoding. The type **String** in Lazarus is by default also UTF-8.
61-
62-So, the string contains more bytes than "characters", since the "lowercase i with accent" is made up of 2 bytes.
63-
64-In UTF-8 all plain ASCII (so up to \#127) are stored as a single byte. All other "characters" are stored as 2, 3 or 4 byte sequences. This makes iteration through a UTF-8 encoded string more complex than old style single byte encoding (ALIAS codepages).
65-
66-The LazUTF8 unit from Lazarus has various functions to handle UTF8 encoded strings. E.g. UTF8Length(): it returns the length in "UTF-8 characters", instead of the length in bytes (as Length() does): Utf8Length('Ä') is 1, whilst Length('Ä') is 2.
67-
68-Displaying UTF-8 encoded strings will be displayed as expected in any visual component of Lazarus.
69-
70-When you write to the console, you have to understand that the console has a different codepage altogether. It can only display 255 different characters, and it treats strings as being single byte encoded. So "i with accent", which consist of 2 bytes is treated as 2 separate chars, and how they look on the console is dependant on your codepage. It will look different e.g. in Dutch locale, than on e.g. a Windows with Russian locale settings.
71-
72-You can fight the Lazarus system and declare your stings as being of a certain codepage and use **RawByteString** to prevent the compiler form doing unwanted codepage conversion. But in the long run, you better go with the flow.
73-
74-Note: I write "character" where I mean the the visual glyph on the screen we normally interpret as being a character (and this probably only holds for Western language, not for e.g. Farsi). The term character is a bit fuzzy when it comes to Unicode.
75-
76-## See also
77-
78-- [[Character and string types]], a detailed reference covering internal memory layout and access options.
79-
80----
81-*From the Free Pascal wiki, page "String", as it stood on 2022-11-01 after a change by Alextp, 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).*
1+#REDIRECT [[String]]

Back to history