Sign in Join
Wiki copytemporary

Character and string types/es: what changed

Revision 2 to 3. 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
3-## Contents
4-
5- * 1 AnsiChar
6- * 1.1 Referencia
7- * 2 WideChar
8- * 2.1 Referencias
9- * 3 Martriz de caracteres (Array of Char)
10- * 3.1 Matriz estática de Char
11- * 3.2 Matriz dinámica de Char
12- * 4 PChar
13- * 4.1 Referencia
14- * 5 PWideChar
15- * 6 Texto de encabezado
16- * 6.1 Referencia
17- * 7 String
18- * 7.1 Reference
19- * 8 ShortString
20- * 8.1 Reference
21- * 9 AnsiString
22- * 9.1 Referencia
23- * 10 UnicodeString
24- * 10.1 Referencia
25- * 11 UTF8String
26- * 11.1 Referencia
27- * 12 UTF16String
28- * 12.1 Reference
29- * 13 WideString
30- * 13.1 Referencia
31- * 14 PShortString
32- * 14.1 Reference
33- * 15 PAnsiString
34- * 15.1 Reference
35- * 16 PUnicodeString
36- * 16.1 Referencia
37- * 17 PWideString
38- * 17.1 Referencías
39- * 18 String constants
40- * 19 See also
41-
423## AnsiChar
434
445Un tipo de variable **AnsiChar** , también referenciado como **char** , es exactamente de 1 byte de tamaño, y contiene un carácter ANSI.
456
467a
478---
489
4910#### Referencia
5011
5112 * [Documentación FPC sobre AnsiChar](<http://www.freepascal.org/docs-html/ref/refsu7.html>)
5213 * [[Char|Uso de Char]]
5314
5415## WideChar
5516
5617Una 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.
5718
5819| a
5920---|---
6021
6122#### Referencias
6223
6324 * [Documentación FPC sobre WideChar](<http://www.freepascal.org/docs-html/ref/refsu8.html>)
6425 * [Información sobre UTF-16 en la Wikipedia](<https://en.wikipedia.org/wiki/UTF-16>)
6526 * [RTL Documentación UnicodeChar](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/unicodechar.html> "doc:rtl/system/unicodechar.html")
6627
6728## Martriz de caracteres (Array of Char)
6829
6930Las 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.
7031
7132### Matriz estática de Char
7233```
7334type
7435 TOldString4 = array[0..3] of char;
7536 var
7637 aOldString4: TOldString4;
7738 begin
7839 aOldString4[0] := 'a';
7940 aOldString4[1] := 'b';
8041 aOldString4[2] := 'c';
8142 aOldString4[3] := 'd';
8243 end;
8344```
8445
8546La matriz estática de caracteres tiene ahora el siguiente contenido:
8647
8748a | b | c | d
8849---|---|---|---
8950
9051[![Note-icon.png](/wiki-images/b/be/Note-icon.png)](</File:Note-icon.png>)
9152
9253**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.
9354
9455### Matriz dinámica de Char
9556```
9657var
9758 aOldString: Array of Char;
9859 begin
9960 SetLength(aOldString, 5);
10061 aOldString[0] := 'a';
10162 aOldString[1] := 'b';
10263 aOldString[2] := 'c';
10364 aOldString[3] := 'd';
10465 end;
10566```
10667
10768La matriz dinámica de caracteres tiene ahora el siguiente contenido:
10869
10970a | b | c | d | #0
11071---|---|---|---|---
11172
11273[![Note-icon.png](/wiki-images/b/be/Note-icon.png)](</File:Note-icon.png>)
11374
11475**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 ...)
11576
11677## PChar
11778
11879Una 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.
11980
12081a | b | c | #0
12182---|---|---|---
12283^
12384
12485#### Referencia
12586
12687 * [Documentación en FPC sobre PChar](<http://www.freepascal.org/docs-html/ref/refsu16.html>)
12788 * [Funciones relacionadas con PChar](<http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/pcharfunctions.html> "doc:rtl/sysutils/pcharfunctions.html")
12889
12990## PWideChar
13091
13192A variable of type **PWideChar** is a pointer to a WideChar variable.
13293
13394| a | | b | | c | #0 | #0
13495---|---|---|---|---|---|---|---
13596^
13697
13798
13899
139100## Texto de encabezado
140101
141102#### Referencia
142103
143104 * [Documentación en la RTL sobre PWideChar](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pwidechar.html> "doc:rtl/system/pwidechar.html")
144105
145106## String
146107
147108El 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**.
148109
149110#### Reference
150111
151112 * [[String|Usage String]]
152113 * [String functions](<http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/stringfunctions.html> "doc:rtl/sysutils/stringfunctions.html")
153114 * [Reference for unit 'strutils': Procedures and functions](<http://lazarus-ccr.sourceforge.net/docs/rtl/strutils/index-5.html> "doc:rtl/strutils/index-5.html")
154115
155116## ShortString
156117
157118Short strings tienen una longitud máxima de 255 caracteres con los [[FPC Unicode support|codepage]] implícitos [[FPC Unicode support|CP_ACP]]
158119
159120La longitud o tamaño se almacena en el caracter de índice 0 (cero).
160121
161122#3 | a | b | c
162123---|---|---|---
163124
164125#### Reference
165126
166127 * [FPC AnsiString documentation](<http://lazarus-ccr.sourceforge.net/docs/ref/refsu12.html> "doc:ref/refsu12.html")
167128
168129## AnsiString
169130
170131Ansistrings 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.
171132
172133| | | | | | | | a | b | c | #0
173134---|---|---|---|---|---|---|---|---|---|---|---
174135RefCount | Length
175136
176137#### Referencia
177138
178139 * [Documentación de FPC sobre AnsiString](<http://www.freepascal.org/docs-html/ref/refsu12.html>)
179140
180141## UnicodeString
181142
182143Al igual que **AnsiStrings** , **UnicodeStrings** son reference counted, matrices terminadas con null, pero son implementadas como matrices de **WideChars** en lugar de **Chars** regulares.
183144
184145[![Note-icon.png](/wiki-images/b/be/Note-icon.png)](</File:Note-icon.png>)
185146
186147**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)...
187148
188149| | | | | | | | | a | | b | | c | #0 | #0
189150---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---
190151RefCount | Length
191152
192153#### Referencia
193154
194155 * [Documentacion en FPC sobre UnicodeString](<http://www.freepascal.org/docs-html/ref/refsu13.html>)
195156
196157## UTF8String
197158
198159En 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:
199160```
200161UTF8String = type AnsiString(CP_UTF8);
201162```
202163
203164Esto significa que contienen cadenas codificadas como UTF-8 (i.e. datos unicode) oscilando entre 1..4 bytes por carácter.
204165
205166Ten en cuenta que un **String** puede también contener caracteres codificados enn UTF-8.
206167
207168#### Referencia
208169
209170 * [Documentación en RTL sobre UTF8String](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/utf8string.html> "doc:rtl/system/utf8string.html")
210171
211172## UTF16String
212173
213174The type **UTF16String** is an alias to the type **WideString**. In the LCL unit _lclproc_ it is an alias to **UnicodeString**.
214175
215176#### Reference
216177
217178 * [LCL UTF16String documentation](<http://lazarus-ccr.sourceforge.net/docs/lcl/lclproc/utf16string.html> "doc:lcl/lclproc/utf16string.html")
218179
219180## WideString
220181
221182Variables 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.
222183
223184WideStrings 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.
224185
225186| | | | | a | | b | | c | #0 | #0
226187---|---|---|---|---|---|---|---|---|---|---|---
227188Length
228189
229190#### Referencia
230191
231192 * [Documentación en FPC sobre WideString](<http://www.freepascal.org/docs-html/ref/refsu14.html#x37-400003.2.8>)
232193
233194## PShortString
234195
235196A 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).
236197
237198#3 | a | b | c
238199---|---|---|---
239200^
240201
241202#### Reference
242203
243204 * [RTL PShortString documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pshortstring.html> "doc:rtl/system/pshortstring.html")
244205
245206## PAnsiString
246207
247208Variables 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**.
248209
249210| | | | | | | | a | b | c | #0
250211---|---|---|---|---|---|---|---|---|---|---|---
251212RefCount | Length | ^
252213
253214#### Reference
254215
255216 * [RTL PAnsiString documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pansistring.html> "doc:rtl/system/pansistring.html")
256217
257218## PUnicodeString
258219
259220Las variables del tipo **PUnicodeString** son punteros a variables del tipo **UnicodeString**.
260221
261222| | | | | | | | | a | | b | | c | #0 | #0
262223---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---
263224RefCount | Length | ^
264225
265226#### Referencia
266227
267228 * [Documentación en la RTL sobre PUnicodeString](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/punicodestring.html> "doc:rtl/system/punicodestring.html")
268229
269230## PWideString
270231
271232Las variables de tipo **PWideString** son punteros. Apuntan al primer carácter de una variable tipo **WideString**.
272233
273234| | | | | a | | b | | c | #0 | #0
274235---|---|---|---|---|---|---|---|---|---|---|---
275236Length | ^
276237
277238#### Referencías
278239
279240 * [Documentación en la RTL sobre PWideString](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pwidestring.html> "doc:rtl/system/pwidestring.html")
280241
281242## String constants
282243
283244If 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.
284245
285246There 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.
286247
287248String 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
288249---|---|---|---|---
289250AnAnsiString:='ãü'; | 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.
290251AnAnsiString:='ãü'; | 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
291252AnUnicodeString:='ãü'; | Without | Wrong everywhere | Wrong everywhere | Wrong everywhere
292253AnUnicodeString:='ãü'; | 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
293254AnUTF8String:='ãü'; | Without | Same as AnsiString | Wrong everywhere | Wrong everywhere
294255AnUTF8String:='ãü'; | 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.
295256
296257 * W-WinAPI = Windows API "W" functions, UTF-16
297258 * A-WinAPI = Windows API non "W" functions, 8bit system code page
298259 * System CP = The 8bit system code page of the OS. For example code page [1252](<http://en.wikipedia.org/wiki/Windows-1252>).
299260
300261```
301262const
302263 c='ãü';
303264 cstring: string = 'ãü'; // see AnAnsiString:='ãü';
304265 var
305266 s: string;
306267 u: UnicodeString;
307268 begin
308269 s:=c; // same as s:='ãü';
309270 s:=cstring; // does not change encoding
310271 u:=c; // same as u:='ãü';
311272 u:=cstring; // fpc 2.6.1: converts from system cp to UTF-16, fpc 2.7.1+: depends on encoding of cstring
312273 end;
313274```
314275
315276## See also
316277
317278 * [[FPC Unicode support]]
318279 * [[LCL Unicode Support]]
319280 * [[TStringList-TStrings Tutorial]]
320281 * [A Brief History of Strings](<http://www.codexterity.com/delphistrings.htm>)
321282
322283---
323284*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