Sign in Join
Wiki copytemporary

Character and string types/es: what changed

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

Title: Character and string types/es

OldNewLine
1+Free 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.
2+
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+
42+## AnsiChar
43+
44+Un tipo de variable **AnsiChar** , también referenciado como **char** , es exactamente de 1 byte de tamaño, y contiene un carácter ANSI.
45+
46+a
47+---
48+
49+#### Referencia
50+
51+ * [Documentación FPC sobre AnsiChar](<http://www.freepascal.org/docs-html/ref/refsu7.html>)
52+ * [[Char|Uso de Char]]
53+
54+## WideChar
55+
56+Una 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.
57+
58+| a
59+---|---
60+
61+#### Referencias
62+
63+ * [Documentación FPC sobre WideChar](<http://www.freepascal.org/docs-html/ref/refsu8.html>)
64+ * [Información sobre UTF-16 en la Wikipedia](<https://en.wikipedia.org/wiki/UTF-16>)
65+ * [RTL Documentación UnicodeChar](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/unicodechar.html> "doc:rtl/system/unicodechar.html")
66+
67+## Martriz de caracteres (Array of Char)
68+
69+Las 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.
70+
71+### Matriz estática de Char
72+```
73+type
74+ TOldString4 = array[0..3] of char;
75+ var
76+ aOldString4: TOldString4;
77+ begin
78+ aOldString4[0] := 'a';
79+ aOldString4[1] := 'b';
80+ aOldString4[2] := 'c';
81+ aOldString4[3] := 'd';
82+ end;
83+```
84+
85+La matriz estática de caracteres tiene ahora el siguiente contenido:
86+
87+a | b | c | d
88+---|---|---|---
89+
90+[![Note-icon.png](/wiki-images/b/be/Note-icon.png)](</File:Note-icon.png>)
91+
92+**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.
93+
94+### Matriz dinámica de Char
95+```
96+var
97+ aOldString: Array of Char;
98+ begin
99+ SetLength(aOldString, 5);
100+ aOldString[0] := 'a';
101+ aOldString[1] := 'b';
102+ aOldString[2] := 'c';
103+ aOldString[3] := 'd';
104+ end;
105+```
106+
107+La matriz dinámica de caracteres tiene ahora el siguiente contenido:
108+
109+a | b | c | d | #0
110+---|---|---|---|---
111+
112+[![Note-icon.png](/wiki-images/b/be/Note-icon.png)](</File:Note-icon.png>)
113+
114+**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 ...)
115+
116+## PChar
117+
118+Una 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.
119+
120+a | b | c | #0
121+---|---|---|---
122+^
123+
124+#### Referencia
125+
126+ * [Documentación en FPC sobre PChar](<http://www.freepascal.org/docs-html/ref/refsu16.html>)
127+ * [Funciones relacionadas con PChar](<http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/pcharfunctions.html> "doc:rtl/sysutils/pcharfunctions.html")
128+
129+## PWideChar
130+
131+A variable of type **PWideChar** is a pointer to a WideChar variable.
132+
133+| a | | b | | c | #0 | #0
134+---|---|---|---|---|---|---|---
135+^
136+
137+
138+
139+## Texto de encabezado
140+
141+#### Referencia
142+
143+ * [Documentación en la RTL sobre PWideChar](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pwidechar.html> "doc:rtl/system/pwidechar.html")
144+
145+## String
146+
147+El 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**.
148+
149+#### Reference
150+
151+ * [[String|Usage String]]
152+ * [String functions](<http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/stringfunctions.html> "doc:rtl/sysutils/stringfunctions.html")
153+ * [Reference for unit 'strutils': Procedures and functions](<http://lazarus-ccr.sourceforge.net/docs/rtl/strutils/index-5.html> "doc:rtl/strutils/index-5.html")
154+
155+## ShortString
156+
157+Short strings tienen una longitud máxima de 255 caracteres con los [[FPC Unicode support|codepage]] implícitos [[FPC Unicode support|CP_ACP]]
158+
159+La longitud o tamaño se almacena en el caracter de índice 0 (cero).
160+
161+#3 | a | b | c
162+---|---|---|---
163+
164+#### Reference
165+
166+ * [FPC AnsiString documentation](<http://lazarus-ccr.sourceforge.net/docs/ref/refsu12.html> "doc:ref/refsu12.html")
167+
168+## AnsiString
169+
170+Ansistrings 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.
171+
172+| | | | | | | | a | b | c | #0
173+---|---|---|---|---|---|---|---|---|---|---|---
174+RefCount | Length
175+
176+#### Referencia
177+
178+ * [Documentación de FPC sobre AnsiString](<http://www.freepascal.org/docs-html/ref/refsu12.html>)
179+
180+## UnicodeString
181+
182+Al igual que **AnsiStrings** , **UnicodeStrings** son reference counted, matrices terminadas con null, pero son implementadas como matrices de **WideChars** en lugar de **Chars** regulares.
183+
184+[![Note-icon.png](/wiki-images/b/be/Note-icon.png)](</File:Note-icon.png>)
185+
186+**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)...
187+
188+| | | | | | | | | a | | b | | c | #0 | #0
189+---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---
190+RefCount | Length
191+
192+#### Referencia
193+
194+ * [Documentacion en FPC sobre UnicodeString](<http://www.freepascal.org/docs-html/ref/refsu13.html>)
195+
196+## UTF8String
197+
198+En 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:
199+```
200+UTF8String = type AnsiString(CP_UTF8);
201+```
202+
203+Esto significa que contienen cadenas codificadas como UTF-8 (i.e. datos unicode) oscilando entre 1..4 bytes por carácter.
204+
205+Ten en cuenta que un **String** puede también contener caracteres codificados enn UTF-8.
206+
207+#### Referencia
208+
209+ * [Documentación en RTL sobre UTF8String](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/utf8string.html> "doc:rtl/system/utf8string.html")
210+
211+## UTF16String
212+
213+The type **UTF16String** is an alias to the type **WideString**. In the LCL unit _lclproc_ it is an alias to **UnicodeString**.
214+
215+#### Reference
216+
217+ * [LCL UTF16String documentation](<http://lazarus-ccr.sourceforge.net/docs/lcl/lclproc/utf16string.html> "doc:lcl/lclproc/utf16string.html")
218+
219+## WideString
220+
221+Variables 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.
222+
223+WideStrings 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.
224+
225+| | | | | a | | b | | c | #0 | #0
226+---|---|---|---|---|---|---|---|---|---|---|---
227+Length
228+
229+#### Referencia
230+
231+ * [Documentación en FPC sobre WideString](<http://www.freepascal.org/docs-html/ref/refsu14.html#x37-400003.2.8>)
232+
233+## PShortString
234+
235+A 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).
236+
237+#3 | a | b | c
238+---|---|---|---
239+^
240+
241+#### Reference
242+
243+ * [RTL PShortString documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pshortstring.html> "doc:rtl/system/pshortstring.html")
244+
245+## PAnsiString
246+
247+Variables 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**.
248+
249+| | | | | | | | a | b | c | #0
250+---|---|---|---|---|---|---|---|---|---|---|---
251+RefCount | Length | ^
252+
253+#### Reference
254+
255+ * [RTL PAnsiString documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pansistring.html> "doc:rtl/system/pansistring.html")
256+
257+## PUnicodeString
258+
259+Las variables del tipo **PUnicodeString** son punteros a variables del tipo **UnicodeString**.
260+
261+| | | | | | | | | a | | b | | c | #0 | #0
262+---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---
263+RefCount | Length | ^
264+
265+#### Referencia
266+
267+ * [Documentación en la RTL sobre PUnicodeString](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/punicodestring.html> "doc:rtl/system/punicodestring.html")
268+
269+## PWideString
270+
271+Las variables de tipo **PWideString** son punteros. Apuntan al primer carácter de una variable tipo **WideString**.
272+
273+| | | | | a | | b | | c | #0 | #0
274+---|---|---|---|---|---|---|---|---|---|---|---
275+Length | ^
276+
277+#### Referencías
278+
279+ * [Documentación en la RTL sobre PWideString](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pwidestring.html> "doc:rtl/system/pwidestring.html")
280+
281+## String constants
282+
283+If 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.
284+
285+There 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.
286+
287+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
288+---|---|---|---|---
289+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.
290+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
291+AnUnicodeString:='ãü'; | Without | Wrong everywhere | Wrong everywhere | Wrong everywhere
292+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
293+AnUTF8String:='ãü'; | Without | Same as AnsiString | Wrong everywhere | Wrong everywhere
294+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.
295+
296+ * W-WinAPI = Windows API "W" functions, UTF-16
297+ * A-WinAPI = Windows API non "W" functions, 8bit system code page
298+ * System CP = The 8bit system code page of the OS. For example code page [1252](<http://en.wikipedia.org/wiki/Windows-1252>).
299+
300+```
301+const
302+ c='ãü';
303+ cstring: string = 'ãü'; // see AnAnsiString:='ãü';
304+ var
305+ s: string;
306+ u: UnicodeString;
307+ begin
308+ s:=c; // same as s:='ãü';
309+ s:=cstring; // does not change encoding
310+ u:=c; // same as u:='ãü';
311+ u:=cstring; // fpc 2.6.1: converts from system cp to UTF-16, fpc 2.7.1+: depends on encoding of cstring
312+ end;
313+```
314+
315+## See also
316+
317+ * [[FPC Unicode support]]
318+ * [[LCL Unicode Support]]
319+ * [[TStringList-TStrings Tutorial]]
320+ * [A Brief History of Strings](<http://www.codexterity.com/delphistrings.htm>)
321+
322+---
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).*

Back to history