Sign in Join
Wiki copytemporary

Character and string types: what changed

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

OldNewLine
11Free Pascal supports several **[[Char|character]] and [[String|string]] types**. They range from single ANSI characters to unicode strings and also include pointer types. Differences also apply to encodings and reference counting.
22
33## Character types
44
55### AnsiChar
66
77A variable of type **AnsiChar**, also referred to as **[[Char|char]]**, is exactly 1 byte in size, and contains one "ANSI" (local code page) character.
88
99| |
1010|-----|
1111| a |
1212
1313#### Reference
1414
1515- [FPC AnsiChar documentation](http://www.freepascal.org/docs-html/ref/refsu6.html)
1616- [[Char|Usage Char]]
1717- [Wikipedia:Windows code page#ANSI code page](https://en.wikipedia.org/wiki/Windows_code_page#ANSI_code_page)
1818
1919### WideChar
2020
2121A variable of type **WideChar**, also referred to as **UnicodeChar**, is exactly 2 bytes in size, and usually contains one [[LCL Unicode Support|Unicode]] code point (normally a character) in UTF-16 encoding. Note: it is impossible to encode all Unicode code points in 2 bytes. Therefore, 2 WideChars may be needed to encode a single code point.
2222
2323| | |
2424|-----|-----|
2525| | a |
2626
2727#### References
2828
2929- [FPC WideChar documentation](http://www.freepascal.org/docs-html/ref/refsu7.html)
3030- [UTF-16 information on Wikipedia](https://en.wikipedia.org/wiki/UTF-16)
3131- [[doc:rtl/system/unicodechar.html|RTL UnicodeChar documentation]]
3232
3333## Character-derived types
3434
3535### Array of Char
3636
3737Early Pascal implementations that were in use before 1978 did not support a string type (with the exception of string constants). The only possibility to store strings in variables was the use of arrays of char. This approach has many disadvantages and is no longer recommended. It is, however, still supported to ensure backward-compatibility with ancient code.
3838
3939#### Static Array of Char
4040
4141``` pascal
4242type
4343 TOldString4 = array[0..3] of char;
4444var
4545 aOldString4: TOldString4;
4646begin
4747 aOldString4[0] := 'a';
4848 aOldString4[1] := 'b';
4949 aOldString4[2] := 'c';
5050 aOldString4[3] := 'd';
5151end;
5252```
5353
5454The static array of char has now the content:
5555
5656| | | | |
5757|-----|-----|-----|-----|
5858| a | b | c | d |
5959
6060> **Note:** Unassigned chars can have any content, depending on what was just in memory when the memory for the array was made available.
6161
6262#### Dynamic Array of Char
6363
6464``` pascal
6565var
6666 aOldString: Array of Char;
6767begin
6868 SetLength(aOldString, 5);
6969 aOldString[0] := 'a';
7070 aOldString[1] := 'b';
7171 aOldString[2] := 'c';
7272 aOldString[3] := 'd';
7373end;
7474```
7575
7676The dynamic array of char has now the content:
7777
7878| | | | | |
7979|-----|-----|-----|-----|-----|
8080| a | b | c | d | \#0 |
8181
8282> **Note:** Unassigned chars in dynamic arrays have a content \#0, cause empty positions of all dynamic arrays are initially initialised with 0 (or \#0, or nil, or ...)
8383
8484### PChar
8585
8686A variable of type **[[PChar]]** is basically a pointer to a **Char** type, but allows additional operations. PChars can be used to access C-style [null-terminated strings](http://en.wikipedia.org/wiki/Null-terminated_string), e.g. in interaction with certain OS libraries or third-party software.
8787
8888| | | | |
8989|-----|-----|-----|-----|
9090| a | b | c | \#0 |
9191| ^ | | | |
9292
9393#### Reference
9494
9595- [FPC PChar documentation](http://www.freepascal.org/docs-html/ref/refsu12.html)
9696- [[doc:rtl/sysutils/pcharfunctions.html|PChar related functions]]
9797
9898### PWideChar
9999
100100A variable of type **PWideChar** is a pointer to a WideChar variable.
101101
102102| | | | | | | | |
103103|-----|-----|-----|-----|-----|-----|-----|-----|
104104| | a | | b | | c | \#0 | \#0 |
105105| ^ | | | | | | | |
106106
107107#### Reference
108108
109109- [[doc:rtl/system/pwidechar.html|RTL PWideChar documentation]]
110110
111111## String types
112112
113113### String
114114
115115The type **String** may refer to **ShortString** or **AnsiString**, depending from the [{\$H} switch](http://www.freepascal.org/docs-html/prog/progsu25.html#x32-310001.2.25). If the switch is off ({\$H-}) then any string declaration will define a **ShortString**. It size will be 255 chars, if not otherwise specified. If it is on ({\$H+}) **string** without length specifier will define an **AnsiString**, otherwise a **ShortString** with specified length. In **mode delphiunicode** **String'' is**UnicodeString'''.
116116
117117#### Reference
118118
119119- [[String|Usage String]].
120120- [[doc:rtl/sysutils/stringfunctions.html|String functions]]
121121- [[doc:rtl/strutils/index-5.html|Reference for unit 'strutils': Procedures and functions]]
122122
123123### ShortString
124124
125125Short strings have a maximum length of 255 characters with the implicit [[FPC Unicode support|codepage]] CP_ACP. The length is stored in the character at index 0. A short string of 255 characters uses 256 bytes of memory (one byte for the length specification and 255 bytes for characters).
126126
127127| | | | |
128128|-----|-----|-----|-----|
129129| \#3 | a | b | c |
130130
131131#### Reference
132132
133133- [FPC Single-byte String types documentation](https://www.freepascal.org/docs-html/ref/refsu9.html)
134134
135135### AnsiString
136136
137137Ansistrings are strings that have no length limit. They are [reference counted](http://en.wikipedia.org/wiki/Reference_counting) and are guaranteed to be [null terminated](http://en.wikipedia.org/wiki/Null-terminated_string). Internally, a variable of type **AnsiString** is treated as a pointer: the actual content of the string is stored on the heap, as much memory as needed to store the string content is allocated.
138138
139-<tbody>
140-
141-</tbody>
139+| | | | | | | | | a | b | c | #0 |
140+|---|---|---|---|---|---|---|---|---|---|---|---|
141+| RefCount | | | | Length | | | | | | | |
142142
143143On 64-bit targets, fields RefCount/Length consume 8 bytes each, not 4.
144144
145145An AnsiString type may also have a compile-time code page since FPC 2.7.1; a missing value defaults to `DefaultSystemCodePage`. A value of `CP_NONE` results in **`RawBytestring`** and a value of `CP_UTF8` results in **`UTF8String`**.
146146
147147#### Reference
148148
149149- [FPC Single-byte String types documentation](https://www.freepascal.org/docs-html/ref/refsu9.html)
150150
151151### UnicodeString
152152
153153Like **AnsiStrings**, **UnicodeStrings** are reference counted, null-terminated arrays, but they are implemented as arrays of **WideChars** instead of regular **Chars**.
154154
155155> **Note:** The UnicodeString naming is a bit ambiguous but probably due to its use in Delphi on Windows, where the OS uses UTF16 encoding; it's not the only string type that can hold Unicode string data (see also UTF8String)...
156156
157-<tbody>
158-
159-</tbody>
157+| | | | | | | | | | a | | b | | c | #0 | #0 |
158+|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
159+| RefCount | | | | Length | | | | | | | | | | | |
160160
161161On 64-bit targets, fields RefCount/Length consume 8 bytes each, not 4.
162162
163163#### Reference
164164
165165- [FPC Multi-byte String types documentation](http://www.freepascal.org/docs-html/ref/refsu10.html)
166166
167167### UTF8String
168168
169169In FPC 2.6.5 and below the type **UTF8String** was an alias to the type **AnsiString**. In FPC 2.7.1 and above it is defined as `UTF8String = type AnsiString(CP_UTF8);`
170170
171171It is meant to contain UTF-8 encoded strings (i.e. unicode data) ranging from 1..4 bytes per character. Note that **String** can also contain UTF-8 encoded characters.
172172
173173#### Reference
174174
175175- [FPC UTF8String documentation](https://www.freepascal.org/docs-html/ref/refsu9.html#x32-400003.2.4)
176176
177177### UTF16String
178178
179179The type **UTF16String** is an alias to the type **WideString**. In the LCL unit *lclproc* it is an alias to **UnicodeString**.
180180
181181#### Reference
182182
183183- [[doc:lcl/lclproc/utf16string.html|LCL UTF16String documentation]]
184184
185185### WideString
186186
187187Variables of type **[[Widestrings|WideString]]** (used to represent unicode character strings in COM applications) resemble those of type **UnicodeString**, but unlike them they are not reference-counted. On Windows they are allocated with a special windows function which allows them to be used for OLE automation.
188188
189189WideStrings consist of COM compatible UTF16 encoded bytes on Windows machines (UCS2 on Windows 2000), and they are encoded as plain UTF16 on Linux, Mac OS X and iOS.
190190
191-<tbody>
192-
193-</tbody>
191+| | | | | | a | | b | | c | #0 | #0 |
192+|---|---|---|---|---|---|---|---|---|---|---|---|
193+| Length | | | | | | | | | | | |
194194
195195#### Reference
196196
197197- [FPC Multi-byte String types documentation](http://www.freepascal.org/docs-html/ref/refsu10.html)
198198
199199## String-derived types
200200
201201### PShortString
202202
203203A variable of type **PShortString** is a pointer that points to the first byte of a **ShortString**-type variable (which defines the length of the ShortString).
204204
205205| | | | |
206206|-----|-----|-----|-----|
207207| \#3 | a | b | c |
208208| ^ | | | |
209209
210210#### Reference
211211
212212- [[doc:rtl/system/pshortstring.html|RTL PShortString documentation]]
213213
214214### PAnsiString
215215
216216Variables of type **PAnsiString** are pointers to **AnsiString**-type variables. However, unlike **PShortString**-type variables they don't point to the first byte of the header, but to the first **char** of the **AnsiString**.
217217
218-<tbody>
219-
220-</tbody>
218+| | | | | | | | | a | b | c | #0 |
219+|---|---|---|---|---|---|---|---|---|---|---|---|
220+| RefCount | | | | Length | | | | ^ | | | |
221221
222222#### Reference
223223
224224- [[doc:rtl/system/pansistring.html|RTL PAnsiString documentation]]
225225
226226### PUnicodeString
227227
228228Variables of type **PUnicodeString** are pointers to variables of type **UnicodeString**.
229229
230-<tbody>
231-
232-</tbody>
230+| | | | | | | | | | a | | b | | c | #0 | #0 |
231+|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
232+| RefCount | | | | Length | | | | ^ | | | | | | | |
233233
234234#### Reference
235235
236236- [[doc:rtl/system/punicodestring.html|RTL PUnicodeString documentation]]
237237
238238### PWideString
239239
240240Variables of type **PWideString** are pointers. They point to the first char of a **WideString**-typed variable.
241241
242-<tbody>
243-
244-</tbody>
242+| | | | | | a | | b | | c | #0 | #0 |
243+|---|---|---|---|---|---|---|---|---|---|---|---|
244+| Length | | | | ^ | | | | | | | |
245245
246246#### Reference
247247
248248- [[doc:rtl/system/pwidestring.html|RTL PWideString documentation]]
249249
250250## String constants
251251
252252If you use only English (ASCII) constants your strings work the same with all types, on all platforms and all compiler versions. Non English strings can be loaded via resourcestrings or from files. If you want to use non English strings in code then you should read further.
253253
254254There are various encodings for non English strings. By default Lazarus saves Pascal files as **UTF-8 without BOM**. UTF-8 supports the full Unicode range. That means all string constants are stored in UTF-8. Lazarus also supports to change the encoding of a file to other encoding, for example under Windows your local codepage. The Windows codepage is limited to your current language group.
255255
256256| String Type, UTF-8 Source | With `{$codepage utf8}`? | FPC ≤ 2.6.5 | FPC ≥ 2.7.1 | FPC ≤ 2.7.1, [[Unicode Support in Lazarus|UTF8 as default CodePage]] |
257257|----|----|----|----|----|
258258| AnAnsiString:='ãü'; | No | Needs UTF8ToAnsi in RTL/WinAPI. Ok in LCL | Needs UTF8ToAnsi in RTL/WinAPI. Ok in LCL | Ok in RTL/W-WinAPI/LCL. Needs UTF8ToWinCP in A-WinAPI. |
259259| AnAnsiString:='ãü'; | Yes | System cp ok in RTL/WinAPI. Needs SysToUTF8 in LCL | Ok in RTL/WinAPI/LCL. Mixing causes conversion. | Ok in RTL/W-WinAPI/LCL. Needs UTF8ToWinCP in A-WinAPI |
260260| AnUnicodeString:='ãü'; | No | Wrong everywhere | Wrong everywhere | Wrong everywhere |
261261| AnUnicodeString:='ãü'; | Yes | System cp ok in RTL/WinAPI. Needs UTF8Encode in LCL | Ok in RTL/WinAPI/LCL.Mixing causes conversion. | Ok in RTL/W-WinAPI/LCL. Needs UTF8ToWinCP in A-WinAPI |
262262| AnUTF8String:='ãü'; | No | Same as AnsiString | Wrong everywhere | Wrong everywhere |
263263| AnUTF8String:='ãü'; | Yes | Same as AnsiString | Ok in RTL/WinAPI/LCL. Mixing causes conversion. | Ok in RTL/W-WinAPI/LCL. Needs UTF8ToWinCP in A-WinAPI. |
264264
265265- W-WinAPI = Windows API "W" functions, UTF-16
266266- A-WinAPI = Windows API non "W" functions, 8bit system ("ANSI") code page
267267- System CP = The 8bit system code page of the OS. For example code page [1252](http://en.wikipedia.org/wiki/Windows-1252).
268268
269269``` pascal
270270const
271271 c='ãü';
272272 cstring: string = 'ãü'; // see AnAnsiString:='ãü';
273273var
274274 s: string;
275275 u: UnicodeString;
276276begin
277277 s:=c; // same as s:='ãü';
278278 s:=cstring; // does not change encoding
279279 u:=c; // same as u:='ãü';
280280 u:=cstring; // fpc 2.6.1: converts from system cp to UTF-16, fpc 2.7.1+: depends on encoding of cstring
281281end;
282282```
283283
284284The rules for conversion are laid out in a ["Code page conversions"](https://freepascal.org/docs-html/ref/refsu9.html#x32-380003.2.4) section in the FPC manual. The basic point is that assigning an AnsiString (including the `CP_UTF8` specialization) to another AnsiString converts what is in the source to match the code page of the target string. A quirk for compatibility with presumably fpc ≤ 2.6.5 is that no such conversion will be done if one matches the source code CP and the other matches the system CP. In this case, forced (likely incorrect) interpretation as the target code page will occur.
285285
286286## See also
287287
288288- [[FPC Unicode support]]
289289- [[LCL Unicode Support]]
290290- [[TStringList-TStrings Tutorial]]
291291- [A Brief History of Strings](http://web.archive.org/web/20151125114234/http://www.codexterity.com/delphistrings.htm)
292292
293293---
294294*From the Free Pascal wiki, page "Character and string types", as it stood on 2023-12-14 after a change by Coz, 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).*

Back to history