Sign in Join
Wiki copytemporary

Character and string types/es: what changed

Revision 1 to 2. 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## Contents
44
55 * 1 AnsiChar
66 * 1.1 Referencia
77 * 2 WideChar
88 * 2.1 Referencias
99 * 3 Martriz de caracteres (Array of Char)
1010 * 3.1 Matriz estática de Char
1111 * 3.2 Matriz dinámica de Char
1212 * 4 PChar
1313 * 4.1 Referencia
1414 * 5 PWideChar
1515 * 6 Texto de encabezado
1616 * 6.1 Referencia
1717 * 7 String
1818 * 7.1 Reference
1919 * 8 ShortString
2020 * 8.1 Reference
2121 * 9 AnsiString
2222 * 9.1 Referencia
2323 * 10 UnicodeString
2424 * 10.1 Referencia
2525 * 11 UTF8String
2626 * 11.1 Referencia
2727 * 12 UTF16String
2828 * 12.1 Reference
2929 * 13 WideString
3030 * 13.1 Referencia
3131 * 14 PShortString
3232 * 14.1 Reference
3333 * 15 PAnsiString
3434 * 15.1 Reference
3535 * 16 PUnicodeString
3636 * 16.1 Referencia
3737 * 17 PWideString
3838 * 17.1 Referencías
3939 * 18 String constants
4040 * 19 See also
4141
4242## AnsiChar
4343
4444Un tipo de variable **AnsiChar** , también referenciado como **char** , es exactamente de 1 byte de tamaño, y contiene un carácter ANSI.
4545
4646a
4747---
4848
4949#### Referencia
5050
5151 * [Documentación FPC sobre AnsiChar](<http://www.freepascal.org/docs-html/ref/refsu7.html>)
5252 * [[Char|Uso de Char]]
5353
5454## WideChar
5555
5656Una 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.
5757
5858| a
5959---|---
6060
6161#### Referencias
6262
6363 * [Documentación FPC sobre WideChar](<http://www.freepascal.org/docs-html/ref/refsu8.html>)
6464 * [Información sobre UTF-16 en la Wikipedia](<https://en.wikipedia.org/wiki/UTF-16>)
6565 * [RTL Documentación UnicodeChar](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/unicodechar.html> "doc:rtl/system/unicodechar.html")
6666
6767## Martriz de caracteres (Array of Char)
6868
6969Las 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.
7070
7171### Matriz estática de Char
7272```
7373type
7474 TOldString4 = array[0..3] of char;
7575 var
7676 aOldString4: TOldString4;
7777 begin
7878 aOldString4[0] := 'a';
7979 aOldString4[1] := 'b';
8080 aOldString4[2] := 'c';
8181 aOldString4[3] := 'd';
8282 end;
8383```
8484
8585La matriz estática de caracteres tiene ahora el siguiente contenido:
8686
8787a | b | c | d
8888---|---|---|---
8989
9090[![Note-icon.png](/wiki-images/b/be/Note-icon.png)](</File:Note-icon.png>)
9191
9292**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.
9393
9494### Matriz dinámica de Char
9595```
9696var
9797 aOldString: Array of Char;
9898 begin
9999 SetLength(aOldString, 5);
100100 aOldString[0] := 'a';
101101 aOldString[1] := 'b';
102102 aOldString[2] := 'c';
103103 aOldString[3] := 'd';
104104 end;
105105```
106106
107107La matriz dinámica de caracteres tiene ahora el siguiente contenido:
108108
109109a | b | c | d | #0
110110---|---|---|---|---
111111
112112[![Note-icon.png](/wiki-images/b/be/Note-icon.png)](</File:Note-icon.png>)
113113
114114**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 ...)
115115
116116## PChar
117117
118118Una 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.
119119
120120a | b | c | #0
121121---|---|---|---
122122^
123123
124124#### Referencia
125125
126126 * [Documentación en FPC sobre PChar](<http://www.freepascal.org/docs-html/ref/refsu16.html>)
127127 * [Funciones relacionadas con PChar](<http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/pcharfunctions.html> "doc:rtl/sysutils/pcharfunctions.html")
128128
129129## PWideChar
130130
131131A variable of type **PWideChar** is a pointer to a WideChar variable.
132132
133133| a | | b | | c | #0 | #0
134134---|---|---|---|---|---|---|---
135135^
136136
137137
138138
139139## Texto de encabezado
140140
141141#### Referencia
142142
143143 * [Documentación en la RTL sobre PWideChar](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pwidechar.html> "doc:rtl/system/pwidechar.html")
144144
145145## String
146146
147147El 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**.
148148
149149#### Reference
150150
151151 * [[String|Usage String]]
152152 * [String functions](<http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/stringfunctions.html> "doc:rtl/sysutils/stringfunctions.html")
153153 * [Reference for unit 'strutils': Procedures and functions](<http://lazarus-ccr.sourceforge.net/docs/rtl/strutils/index-5.html> "doc:rtl/strutils/index-5.html")
154154
155155## ShortString
156156
157157Short strings tienen una longitud máxima de 255 caracteres con los [[FPC Unicode support|codepage]] implícitos [[FPC Unicode support|CP_ACP]]
158158
159159La longitud o tamaño se almacena en el caracter de índice 0 (cero).
160160
161161#3 | a | b | c
162162---|---|---|---
163163
164164#### Reference
165165
166166 * [FPC AnsiString documentation](<http://lazarus-ccr.sourceforge.net/docs/ref/refsu12.html> "doc:ref/refsu12.html")
167167
168168## AnsiString
169169
170170Ansistrings 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.
171171
172172| | | | | | | | a | b | c | #0
173173---|---|---|---|---|---|---|---|---|---|---|---
174174RefCount | Length
175175
176176#### Referencia
177177
178178 * [Documentación de FPC sobre AnsiString](<http://www.freepascal.org/docs-html/ref/refsu12.html>)
179179
180180## UnicodeString
181181
182182Al igual que **AnsiStrings** , **UnicodeStrings** son reference counted, matrices terminadas con null, pero son implementadas como matrices de **WideChars** en lugar de **Chars** regulares.
183183
184184[![Note-icon.png](/wiki-images/b/be/Note-icon.png)](</File:Note-icon.png>)
185185
186186**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)...
187187
188188| | | | | | | | | a | | b | | c | #0 | #0
189189---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---
190190RefCount | Length
191191
192192#### Referencia
193193
194194 * [Documentacion en FPC sobre UnicodeString](<http://www.freepascal.org/docs-html/ref/refsu13.html>)
195195
196196## UTF8String
197197
198198En 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:
199199```
200200UTF8String = type AnsiString(CP_UTF8);
201201```
202202
203203Esto significa que contienen cadenas codificadas como UTF-8 (i.e. datos unicode) oscilando entre 1..4 bytes por carácter.
204204
205205Ten en cuenta que un **String** puede también contener caracteres codificados enn UTF-8.
206206
207207#### Referencia
208208
209209 * [Documentación en RTL sobre UTF8String](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/utf8string.html> "doc:rtl/system/utf8string.html")
210210
211211## UTF16String
212212
213213The type **UTF16String** is an alias to the type **WideString**. In the LCL unit _lclproc_ it is an alias to **UnicodeString**.
214214
215215#### Reference
216216
217217 * [LCL UTF16String documentation](<http://lazarus-ccr.sourceforge.net/docs/lcl/lclproc/utf16string.html> "doc:lcl/lclproc/utf16string.html")
218218
219219## WideString
220220
221221Variables 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.
222222
223223WideStrings 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.
224224
225225| | | | | a | | b | | c | #0 | #0
226226---|---|---|---|---|---|---|---|---|---|---|---
227227Length
228228
229229#### Referencia
230230
231231 * [Documentación en FPC sobre WideString](<http://www.freepascal.org/docs-html/ref/refsu14.html#x37-400003.2.8>)
232232
233233## PShortString
234234
235235A 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).
236236
237237#3 | a | b | c
238238---|---|---|---
239239^
240240
241241#### Reference
242242
243243 * [RTL PShortString documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pshortstring.html> "doc:rtl/system/pshortstring.html")
244244
245245## PAnsiString
246246
247247Variables 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**.
248248
249249| | | | | | | | a | b | c | #0
250250---|---|---|---|---|---|---|---|---|---|---|---
251251RefCount | Length | ^
252252
253253#### Reference
254254
255255 * [RTL PAnsiString documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pansistring.html> "doc:rtl/system/pansistring.html")
256256
257257## PUnicodeString
258258
259259Las variables del tipo **PUnicodeString** son punteros a variables del tipo **UnicodeString**.
260260
261261| | | | | | | | | a | | b | | c | #0 | #0
262262---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---
263263RefCount | Length | ^
264264
265265#### Referencia
266266
267267 * [Documentación en la RTL sobre PUnicodeString](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/punicodestring.html> "doc:rtl/system/punicodestring.html")
268268
269269## PWideString
270270
271271Las variables de tipo **PWideString** son punteros. Apuntan al primer carácter de una variable tipo **WideString**.
272272
273273| | | | | a | | b | | c | #0 | #0
274274---|---|---|---|---|---|---|---|---|---|---|---
275275Length | ^
276276
277277#### Referencías
278278
279279 * [Documentación en la RTL sobre PWideString](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pwidestring.html> "doc:rtl/system/pwidestring.html")
280280
281281## String constants
282282
283283If 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.
284284
285285There 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.
286286
287287String 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
288288---|---|---|---|---
289289AnAnsiString:='ãü'; | 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.
290290AnAnsiString:='ãü'; | 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
291291AnUnicodeString:='ãü'; | Without | Wrong everywhere | Wrong everywhere | Wrong everywhere
292292AnUnicodeString:='ãü'; | 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
293293AnUTF8String:='ãü'; | Without | Same as AnsiString | Wrong everywhere | Wrong everywhere
294294AnUTF8String:='ãü'; | 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.
295295
296296 * W-WinAPI = Windows API "W" functions, UTF-16
297297 * A-WinAPI = Windows API non "W" functions, 8bit system code page
298298 * System CP = The 8bit system code page of the OS. For example code page [1252](<http://en.wikipedia.org/wiki/Windows-1252>).
299299
300300```
301301const
302302 c='ãü';
303303 cstring: string = 'ãü'; // see AnAnsiString:='ãü';
304304 var
305305 s: string;
306306 u: UnicodeString;
307307 begin
308308 s:=c; // same as s:='ãü';
309309 s:=cstring; // does not change encoding
310310 u:=c; // same as u:='ãü';
311311 u:=cstring; // fpc 2.6.1: converts from system cp to UTF-16, fpc 2.7.1+: depends on encoding of cstring
312312 end;
313313```
314314
315315## See also
316316
317317 * [[FPC Unicode support]]
318318 * [[LCL Unicode Support]]
319319 * [[TStringList-TStrings Tutorial]]
320320 * [A Brief History of Strings](<http://www.codexterity.com/delphistrings.htm>)
321321
322322---
323-*From the Free Pascal wiki, page "Character and string types/es", as the Internet Archive kept it on 2025-01-19. 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).*
323+*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