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