Sign in Join
Wiki copytemporary

Character and string types/es: what changed

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

OldNewLine
11Free Pascal soporta varios tipos de **[[Char|character]] and [[String|string]]**. Estos van desde simples caracteres ANSI a cadenas UNICODE y además incluye los tipos puntero. Las diferencias también se aplican a las codificaciones y reference counting.
22
33## AnsiChar
44
55Un tipo de variable **AnsiChar** , también referenciado como **char** , es exactamente de 1 byte de tamaño, y contiene un carácter ANSI.
66
7-a
8----
9-
7+| a |
8+|---|
9+
1010#### Referencia
1111
1212 * [Documentación FPC sobre AnsiChar](<http://www.freepascal.org/docs-html/ref/refsu7.html>)
1313 * [[Char|Uso de Char]]
1414
1515## WideChar
1616
1717Una variable de tipo **WideChar** , también referenciada como **UnicodeChar** , es exactamente de 2 bytes de tamaño, y contieen un (parte de) [[LCL Unicode Support|Unicode]] character in UTF-16 encoding. Nota: es imposible codificar todos los puntos codificables Unicode con 2 bytes. Therefore, se pueden necesitar 2 WideChars para codificar un solo code point.
1818
19-| a
20----|---
21-
19+| | a |
20+|---|---|
21+
2222#### Referencias
2323
2424 * [Documentación FPC sobre WideChar](<http://www.freepascal.org/docs-html/ref/refsu8.html>)
2525 * [Información sobre UTF-16 en la Wikipedia](<https://en.wikipedia.org/wiki/UTF-16>)
2626 * [RTL Documentación UnicodeChar](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/unicodechar.html> "doc:rtl/system/unicodechar.html")
2727
2828## Martriz de caracteres (Array of Char)
2929
3030Las implementaciones tempranas de Pascal existentes antes de 1978 no soportaba el tipo **string** (con la excepción de las constantes cadena). La única posibilidad de almacenar cadenas en variables era utilizar matrices de caracteres (array of char). Esta aproximación tiene bastantes desventajas y se desaconseja su uso. Se soporta todavía para asegurar la compatibilidad con código antiguo.
3131
3232### Matriz estática de Char
3333```
3434type
3535 TOldString4 = array[0..3] of char;
3636 var
3737 aOldString4: TOldString4;
3838 begin
3939 aOldString4[0] := 'a';
4040 aOldString4[1] := 'b';
4141 aOldString4[2] := 'c';
4242 aOldString4[3] := 'd';
4343 end;
4444```
4545
4646La matriz estática de caracteres tiene ahora el siguiente contenido:
4747
48-a | b | c | d
49----|---|---|---
50-
48+| a | b | c | d |
49+|---|---|---|---|
50+
5151[![Note-icon.png](/wiki-images/b/be/Note-icon.png)](</File:Note-icon.png>)
5252
5353**Nota:** Los chars no asignados pueden tener cualquier contenido, dependiendo de lo que exista en ese área de memoria cuando se haga disponible la matriz.
5454
5555### Matriz dinámica de Char
5656```
5757var
5858 aOldString: Array of Char;
5959 begin
6060 SetLength(aOldString, 5);
6161 aOldString[0] := 'a';
6262 aOldString[1] := 'b';
6363 aOldString[2] := 'c';
6464 aOldString[3] := 'd';
6565 end;
6666```
6767
6868La matriz dinámica de caracteres tiene ahora el siguiente contenido:
6969
70-a | b | c | d | #0
71----|---|---|---|---
72-
70+| a | b | c | d | #0 |
71+|---|---|---|---|---|
72+
7373[![Note-icon.png](/wiki-images/b/be/Note-icon.png)](</File:Note-icon.png>)
7474
7575**Nota:** los Chars no asignados en matrices dinámicas tienen como contenido #0, causando que todas las posiciones vacias de las matrices dinámicas sean inicializados al principio con 0 (o #0, o nil, o ...)
7676
7777## PChar
7878
7979Una variable de tipo **PChar** es básicamente un puntero a un tipo **Char** , pero que permite operaciones adicionales. Pchars se pueden utilizar para acceder al estilo C [null-terminated strings](<http://en.wikipedia.org/wiki/Null-terminated_string>), e.g. en interacciones con ciertas librerías del Sistema Operativo o en software de terceros.
8080
81-a | b | c | #0
82----|---|---|---
83-^
84-
81+| a | b | c | #0 |
82+|---|---|---|---|
83+| ^ | | | |
84+
8585#### Referencia
8686
8787 * [Documentación en FPC sobre PChar](<http://www.freepascal.org/docs-html/ref/refsu16.html>)
8888 * [Funciones relacionadas con PChar](<http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/pcharfunctions.html> "doc:rtl/sysutils/pcharfunctions.html")
8989
9090## PWideChar
9191
9292A variable of type **PWideChar** is a pointer to a WideChar variable.
9393
94-| a | | b | | c | #0 | #0
95----|---|---|---|---|---|---|---
96-^
97-
94+| | a | | b | | c | #0 | #0 |
95+|---|---|---|---|---|---|---|---|
96+| ^ | | | | | | | |
97+
9898
9999
100100## Texto de encabezado
101101
102102#### Referencia
103103
104104 * [Documentación en la RTL sobre PWideChar](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pwidechar.html> "doc:rtl/system/pwidechar.html")
105105
106106## String
107107
108108El tipo **String** puede hacer referencia a **ShortString** o **AnsiString** , dependiendo del [{$H} interruptor](<http://www.freepascal.org/docs-html/prog/progsu25.html#x32-310001.2.25>). Si el interruptor está en off ({$H-}) entonces cualquier declaración de string se definirá como **ShortString**. El tamaño será de 255 caracteres, si no está especificado de otro modo. Si el interruptor está en on ({$H+}) un **string** sin especificación de tamaño será definido como un **AnsiString** , caso contrario (especificando el tamaño) será definido como **ShortString** con el tamaño especificado. En **mode delphiunicode** **String** es **UnicodeString**.
109109
110110#### Reference
111111
112112 * [[String|Usage String]]
113113 * [String functions](<http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/stringfunctions.html> "doc:rtl/sysutils/stringfunctions.html")
114114 * [Reference for unit 'strutils': Procedures and functions](<http://lazarus-ccr.sourceforge.net/docs/rtl/strutils/index-5.html> "doc:rtl/strutils/index-5.html")
115115
116116## ShortString
117117
118118Short strings tienen una longitud máxima de 255 caracteres con los [[FPC Unicode support|codepage]] implícitos [[FPC Unicode support|CP_ACP]]
119119
120120La longitud o tamaño se almacena en el caracter de índice 0 (cero).
121121
122-#3 | a | b | c
123----|---|---|---
124-
122+| #3 | a | b | c |
123+|---|---|---|---|
124+
125125#### Reference
126126
127127 * [FPC AnsiString documentation](<http://lazarus-ccr.sourceforge.net/docs/ref/refsu12.html> "doc:ref/refsu12.html")
128128
129129## AnsiString
130130
131131Ansistrings son cadenas que no tienen límite de longitud. Son [reference counted](<http://en.wikipedia.org/wiki/Reference_counting>) y se garantiza que son terminadas con [null](<http://en.wikipedia.org/wiki/Null-terminated_string>). Internamente, una variable de tipo **AnsiString** se trata como un puntero: el actual contenido de la cadena se almacena en montón (heap), se ubica tanta cantidad de memoria como sea necesaria.
132132
133-| | | | | | | | a | b | c | #0
134----|---|---|---|---|---|---|---|---|---|---|---
135-RefCount | Length
136-
133+| | | | | | | | | a | b | c | #0 |
134+|---|---|---|---|---|---|---|---|---|---|---|---|
135+| RefCount | | | | Length | | | | | | | |
136+
137137#### Referencia
138138
139139 * [Documentación de FPC sobre AnsiString](<http://www.freepascal.org/docs-html/ref/refsu12.html>)
140140
141141## UnicodeString
142142
143143Al igual que **AnsiStrings** , **UnicodeStrings** son reference counted, matrices terminadas con null, pero son implementadas como matrices de **WideChars** en lugar de **Chars** regulares.
144144
145145[![Note-icon.png](/wiki-images/b/be/Note-icon.png)](</File:Note-icon.png>)
146146
147147**Nota:** La nomenclatura UnicodeString es un poco ambigua debido a su uso en Delphi bajo Windows, donde los sistemas operativos utilizan cofificación UTF16; no es el único tipo que puede albergar cadenas de datos Unicode (ver también UTF8String)...
148148
149-| | | | | | | | | a | | b | | c | #0 | #0
150----|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---
151-RefCount | Length
152-
149+| | | | | | | | | | a | | b | | c | #0 | #0 |
150+|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
151+| RefCount | | | | Length | | | | | | | | | | | |
152+
153153#### Referencia
154154
155155 * [Documentacion en FPC sobre UnicodeString](<http://www.freepascal.org/docs-html/ref/refsu13.html>)
156156
157157## UTF8String
158158
159159En la versión FPC 2.6.5 e inferiores el tipo **UTF8String** era un alias del tipo **AnsiString**. En FPC 2.7.1 y posteriores se define como:
160160```
161161UTF8String = type AnsiString(CP_UTF8);
162162```
163163
164164Esto significa que contienen cadenas codificadas como UTF-8 (i.e. datos unicode) oscilando entre 1..4 bytes por carácter.
165165
166166Ten en cuenta que un **String** puede también contener caracteres codificados enn UTF-8.
167167
168168#### Referencia
169169
170170 * [Documentación en RTL sobre UTF8String](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/utf8string.html> "doc:rtl/system/utf8string.html")
171171
172172## UTF16String
173173
174174The type **UTF16String** is an alias to the type **WideString**. In the LCL unit _lclproc_ it is an alias to **UnicodeString**.
175175
176176#### Reference
177177
178178 * [LCL UTF16String documentation](<http://lazarus-ccr.sourceforge.net/docs/lcl/lclproc/utf16string.html> "doc:lcl/lclproc/utf16string.html")
179179
180180## WideString
181181
182182Variables 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.
183183
184184WideStrings 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.
185185
186-| | | | | a | | b | | c | #0 | #0
187----|---|---|---|---|---|---|---|---|---|---|---
188-Length
189-
186+| | | | | | a | | b | | c | #0 | #0 |
187+|---|---|---|---|---|---|---|---|---|---|---|---|
188+| Length | | | | | | | | | | | |
189+
190190#### Referencia
191191
192192 * [Documentación en FPC sobre WideString](<http://www.freepascal.org/docs-html/ref/refsu14.html#x37-400003.2.8>)
193193
194194## PShortString
195195
196196A 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).
197197
198-#3 | a | b | c
199----|---|---|---
200-^
201-
198+| #3 | a | b | c |
199+|---|---|---|---|
200+| ^ | | | |
201+
202202#### Reference
203203
204204 * [RTL PShortString documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pshortstring.html> "doc:rtl/system/pshortstring.html")
205205
206206## PAnsiString
207207
208208Variables 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**.
209209
210-| | | | | | | | a | b | c | #0
211----|---|---|---|---|---|---|---|---|---|---|---
212-RefCount | Length | ^
213-
210+| | | | | | | | | a | b | c | #0 |
211+|---|---|---|---|---|---|---|---|---|---|---|---|
212+| RefCount | | | | Length | | | | ^ | | | |
213+
214214#### Reference
215215
216216 * [RTL PAnsiString documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pansistring.html> "doc:rtl/system/pansistring.html")
217217
218218## PUnicodeString
219219
220220Las variables del tipo **PUnicodeString** son punteros a variables del tipo **UnicodeString**.
221221
222-| | | | | | | | | a | | b | | c | #0 | #0
223----|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---
224-RefCount | Length | ^
225-
222+| | | | | | | | | | a | | b | | c | #0 | #0 |
223+|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
224+| RefCount | | | | Length | | | | ^ | | | | | | | |
225+
226226#### Referencia
227227
228228 * [Documentación en la RTL sobre PUnicodeString](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/punicodestring.html> "doc:rtl/system/punicodestring.html")
229229
230230## PWideString
231231
232232Las variables de tipo **PWideString** son punteros. Apuntan al primer carácter de una variable tipo **WideString**.
233233
234-| | | | | a | | b | | c | #0 | #0
235----|---|---|---|---|---|---|---|---|---|---|---
236-Length | ^
237-
234+| | | | | | a | | b | | c | #0 | #0 |
235+|---|---|---|---|---|---|---|---|---|---|---|---|
236+| Length | | | | ^ | | | | | | | |
237+
238238#### Referencías
239239
240240 * [Documentación en la RTL sobre PWideString](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pwidestring.html> "doc:rtl/system/pwidestring.html")
241241
242242## String constants
243243
244244If you use only English 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.
245245
246246There 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.
247247
248-String Type, UTF-8 Source | With or without {$codepage utf8} | FPC 2.6.5 and below | FPC 2.7.1 and above | FPC 2.7.1+ with UTF8 as default CodePage
249----|---|---|---|---
250-AnAnsiString:='ãü'; | Without | 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.
251-AnAnsiString:='ãü'; | With | System cp ok in RTL/WinAPI. Needs SysToUTF8 in LCL | Ok in RTL/WinAPI/LCL. Mixing with other strings converts to system cp | Ok in RTL/W-WinAPI/LCL. Needs UTF8ToWinCP in A-WinAPI
252-AnUnicodeString:='ãü'; | Without | Wrong everywhere | Wrong everywhere | Wrong everywhere
253-AnUnicodeString:='ãü'; | With | System cp ok in RTL/WinAPI. Needs UTF8Encode in LCL | Ok in RTL/WinAPI/LCL. Mixing with other strings converts to system cp | Ok in RTL/W-WinAPI/LCL. Needs UTF8ToWinCP in A-WinAPI
254-AnUTF8String:='ãü'; | Without | Same as AnsiString | Wrong everywhere | Wrong everywhere
255-AnUTF8String:='ãü'; | With | Same as AnsiString | Ok in RTL/WinAPI/LCL. Mixing with other strings converts to system cp | Ok in RTL/W-WinAPI/LCL. Needs UTF8ToWinCP in A-WinAPI.
256-
248+| String Type, UTF-8 Source | With or without {$codepage utf8} | FPC 2.6.5 and below | FPC 2.7.1 and above | FPC 2.7.1+ with UTF8 as default CodePage |
249+|---|---|---|---|---|
250+| AnAnsiString:='ãü'; | Without | 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. |
251+| AnAnsiString:='ãü'; | With | System cp ok in RTL/WinAPI. Needs SysToUTF8 in LCL | Ok in RTL/WinAPI/LCL. Mixing with other strings converts to system cp | Ok in RTL/W-WinAPI/LCL. Needs UTF8ToWinCP in A-WinAPI |
252+| AnUnicodeString:='ãü'; | Without | Wrong everywhere | Wrong everywhere | Wrong everywhere |
253+| AnUnicodeString:='ãü'; | With | System cp ok in RTL/WinAPI. Needs UTF8Encode in LCL | Ok in RTL/WinAPI/LCL. Mixing with other strings converts to system cp | Ok in RTL/W-WinAPI/LCL. Needs UTF8ToWinCP in A-WinAPI |
254+| AnUTF8String:='ãü'; | Without | Same as AnsiString | Wrong everywhere | Wrong everywhere |
255+| AnUTF8String:='ãü'; | With | Same as AnsiString | Ok in RTL/WinAPI/LCL. Mixing with other strings converts to system cp | Ok in RTL/W-WinAPI/LCL. Needs UTF8ToWinCP in A-WinAPI. |
256+
257257 * W-WinAPI = Windows API "W" functions, UTF-16
258258 * A-WinAPI = Windows API non "W" functions, 8bit system code page
259259 * System CP = The 8bit system code page of the OS. For example code page [1252](<http://en.wikipedia.org/wiki/Windows-1252>).
260260
261261```
262262const
263263 c='ãü';
264264 cstring: string = 'ãü'; // see AnAnsiString:='ãü';
265265 var
266266 s: string;
267267 u: UnicodeString;
268268 begin
269269 s:=c; // same as s:='ãü';
270270 s:=cstring; // does not change encoding
271271 u:=c; // same as u:='ãü';
272272 u:=cstring; // fpc 2.6.1: converts from system cp to UTF-16, fpc 2.7.1+: depends on encoding of cstring
273273 end;
274274```
275275
276276## See also
277277
278278 * [[FPC Unicode support]]
279279 * [[LCL Unicode Support]]
280280 * [[TStringList-TStrings Tutorial]]
281281 * [A Brief History of Strings](<http://www.codexterity.com/delphistrings.htm>)
282-
283282---
284283*From the Free Pascal wiki, page "Character and string types/es", as the Internet Archive kept it on 2025-01-19, last changed on 2020-02-11. 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