Character and string types/ja: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | Free Pascal は、各種の **[[Char/ja|文字]] と [[String/ja|文字列]]** を扱えます。それらは ANSIキャラクタ から ユニコード文字列、また 各種ポインタも含みます。違いは、encodings と reference counting にも影響します。 | |
| 2 | 2 | ||
| 3 | 3 | ## AnsiChar | |
| 4 | 4 | ||
| 5 | 5 | A variable of type **AnsiChar** , also referred to as **[[Char|char]]** , is exactly 1 byte in size, and contains one ANSI character. | |
| 6 | 6 | ||
| 7 | - | ||
| 8 | - | ||
| 9 | - | ||
| 7 | + | | a | | |
| 8 | + | |---| | |
| 9 | + | ||
| 10 | 10 | #### Reference | |
| 11 | 11 | ||
| 12 | 12 | * [FPC AnsiChar documentation](<http://www.freepascal.org/docs-html/ref/refsu7.html>) | |
| 13 | 13 | * [[Char|Usage Char]] | |
| 14 | 14 | ||
| 15 | 15 | ## WideChar | |
| 16 | 16 | ||
| 17 | 17 | A variable of type **WideChar** , also referred to as **UnicodeChar** , is exactly 2 bytes in size, and usually contains one [[LCL Unicode Support|Unicode]] code point (normally a character) in UTF-16 encoding. Note: it is impossible to encode all Unicode code points in 2 bytes. Therefore, 2 WideChars may be needed to encode a single code point. | |
| 18 | 18 | ||
| 19 | - | ||
| 20 | - | ||
| 21 | - | ||
| 19 | + | | | a | | |
| 20 | + | |---|---| | |
| 21 | + | ||
| 22 | 22 | #### References | |
| 23 | 23 | ||
| 24 | 24 | * [FPC WideChar documentation](<http://www.freepascal.org/docs-html/ref/refsu8.html>) | |
| 25 | 25 | * [UTF-16 information on Wikipedia](<https://en.wikipedia.org/wiki/UTF-16>) | |
| 26 | 26 | * [RTL UnicodeChar documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/unicodechar.html> "doc:rtl/system/unicodechar.html") | |
| 27 | 27 | ||
| 28 | 28 | ## Array of Char | |
| 29 | 29 | ||
| 30 | 30 | Early Pascal implementations that were in use before 1978 did not support a string type (with the exception of string constants). The only possibility to store strings in variables was the use of arrays of char. This approach has many disadvantages and is no longer recommended. It is, however, still supported to ensure backward-compatibility with ancient code. | |
| 31 | 31 | ||
| 32 | 32 | ### Static Array of 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 | The static array of char has now the content: | |
| 47 | 47 | ||
| 48 | - | ||
| 49 | - | ||
| 50 | - | ||
| 48 | + | | a | b | c | d | | |
| 49 | + | |---|---|---|---| | |
| 50 | + | ||
| 51 | 51 | [](</File:Note-icon.png>) | |
| 52 | 52 | ||
| 53 | 53 | **Note:** Unassigned chars can have any content, depending on what was just in memory when the memory for the array was made available. | |
| 54 | 54 | ||
| 55 | 55 | ### Dynamic Array of 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 | The dynamic array of char has now the content: | |
| 69 | 69 | ||
| 70 | - | ||
| 71 | - | ||
| 72 | - | ||
| 70 | + | | a | b | c | d | #0 | | |
| 71 | + | |---|---|---|---|---| | |
| 72 | + | ||
| 73 | 73 | [](</File:Note-icon.png>) | |
| 74 | 74 | ||
| 75 | 75 | **Note:** Unassigned chars in dynamic arrays have a content #0, cause empty positions of all dynamic arrays are initially initialised with 0 (or #0, or nil, or ...) | |
| 76 | 76 | ||
| 77 | 77 | ## PChar | |
| 78 | 78 | ||
| 79 | 79 | A variable of type **[[PChar]]** is basically a pointer to a **Char** type, but allows additional operations. PChars can be used to access C-style [null-terminated strings](<http://en.wikipedia.org/wiki/Null-terminated_string>), e.g. in interaction with certain OS libraries or third-party software. | |
| 80 | 80 | ||
| 81 | - | ||
| 82 | - | ||
| 83 | - | ||
| 84 | - | ||
| 81 | + | | a | b | c | #0 | | |
| 82 | + | |---|---|---|---| | |
| 83 | + | | ^ | | | | | |
| 84 | + | ||
| 85 | 85 | #### Reference | |
| 86 | 86 | ||
| 87 | 87 | * [FPC PChar documentation](<http://www.freepascal.org/docs-html/ref/refsu13.html>) | |
| 88 | 88 | * [PChar related functions](<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 | #### Reference | |
| 99 | 99 | ||
| 100 | 100 | * [RTL PWideChar documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pwidechar.html> "doc:rtl/system/pwidechar.html") | |
| 101 | 101 | ||
| 102 | 102 | ## String | |
| 103 | 103 | ||
| 104 | 104 | The type **String** may refer to **ShortString** or **AnsiString** , depending from the [{$H} switch](<http://www.freepascal.org/docs-html/prog/progsu25.html#x32-310001.2.25>). If the switch is off ({$H-}) then any string declaration will define a **ShortString**. It size will be 255 chars, if not otherwise specified. If it is on ({$H+}) **string** without length specifier will define an **AnsiString** , otherwise a **ShortString** with specified length. In **mode delphiunicode'**_String_ is **UnicodeString**. | |
| 105 | 105 | ||
| 106 | 106 | #### Reference | |
| 107 | 107 | ||
| 108 | 108 | * [[String|Usage String]] | |
| 109 | 109 | * [String functions](<http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/stringfunctions.html> "doc:rtl/sysutils/stringfunctions.html") | |
| 110 | 110 | * [Reference for unit 'strutils': Procedures and functions](<http://lazarus-ccr.sourceforge.net/docs/rtl/strutils/index-5.html> "doc:rtl/strutils/index-5.html") | |
| 111 | 111 | ||
| 112 | 112 | ## ShortString | |
| 113 | 113 | ||
| 114 | 114 | Short strings have a maximum length of 255 characters with the implicit [[FPC Unicode support|codepage]] CP_ACP. The length is stored in the character at index 0. | |
| 115 | 115 | ||
| 116 | - | ||
| 117 | - | ||
| 118 | - | ||
| 116 | + | | #3 | a | b | c | | |
| 117 | + | |---|---|---|---| | |
| 118 | + | ||
| 119 | 119 | #### Reference | |
| 120 | 120 | ||
| 121 | 121 | * [FPC AnsiString documentation](<http://www.freepascal.org/docs-html/ref/refsu10.html>) | |
| 122 | 122 | ||
| 123 | 123 | ## AnsiString | |
| 124 | 124 | ||
| 125 | 125 | Ansistrings are strings that have no length limit. They are [reference counted](<http://en.wikipedia.org/wiki/Reference_counting>) and are guaranteed to be [null terminated](<http://en.wikipedia.org/wiki/Null-terminated_string>). Internally, a variable of type **AnsiString** is treated as a pointer: the actual content of the string is stored on the heap, as much memory as needed to store the string content is allocated. | |
| 126 | 126 | ||
| 127 | - | ||
| 128 | - | ||
| 129 | - | ||
| 130 | - | ||
| 127 | + | | | | | | | | | | a | b | c | #0 | | |
| 128 | + | |---|---|---|---|---|---|---|---|---|---|---|---| | |
| 129 | + | | RefCount | | | | Length | | | | | | | | | |
| 130 | + | ||
| 131 | 131 | #### Reference | |
| 132 | 132 | ||
| 133 | 133 | * [FPC AnsiString documentation](<http://www.freepascal.org/docs-html/ref/refsu10.html>) | |
| 134 | 134 | ||
| 135 | 135 | ## UnicodeString | |
| 136 | 136 | ||
| 137 | 137 | Like **AnsiStrings** , **UnicodeStrings** are reference counted, null-terminated arrays, but they are implemented as arrays of **WideChars** instead of regular **Chars**. | |
| 138 | 138 | ||
| 139 | 139 | [](</File:Note-icon.png>) | |
| 140 | 140 | ||
| 141 | 141 | **Note:** The UnicodeString naming is a bit ambiguous but probably due to its use in Delphi on Windows, where the OS uses UTF16 encoding; it's not the only string type that can hold Unicode string data (see also UTF8String)... | |
| 142 | 142 | ||
| 143 | - | ||
| 144 | - | ||
| 145 | - | ||
| 146 | - | ||
| 143 | + | | | | | | | | | | | a | | b | | c | #0 | #0 | | |
| 144 | + | |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---| | |
| 145 | + | | RefCount | | | | Length | | | | | | | | | | | | | |
| 146 | + | ||
| 147 | 147 | #### Reference | |
| 148 | 148 | ||
| 149 | 149 | * [FPC UnicodeString documentation](<http://www.freepascal.org/docs-html/ref/refsu11.html>) | |
| 150 | 150 | ||
| 151 | 151 | ## UTF8String | |
| 152 | 152 | ||
| 153 | 153 | In FPC 2.6.5 and below the type **UTF8String** was an alias to the type **AnsiString**. In FPC 2.7.1 and above it is defined as | |
| 154 | 154 | ``` | |
| 155 | 155 | UTF8String = type AnsiString(CP_UTF8); | |
| 156 | 156 | ``` | |
| 157 | 157 | ||
| 158 | 158 | It is meant to contain UTF-8 encoded strings (i.e. unicode data) ranging from 1..4 bytes per character. Note that **String** can also contain UTF-8 encoded characters. | |
| 159 | 159 | ||
| 160 | 160 | #### Reference | |
| 161 | 161 | ||
| 162 | 162 | * [FPC UTF8String documentation](<http://www.freepascal.org/docs-html/ref/refsu10.html#x33-410003.2.4>) | |
| 163 | 163 | ||
| 164 | 164 | ## UTF16String | |
| 165 | 165 | ||
| 166 | 166 | The type **UTF16String** is an alias to the type **WideString**. In the LCL unit _lclproc_ it is an alias to **UnicodeString**. | |
| 167 | 167 | ||
| 168 | 168 | #### Reference | |
| 169 | 169 | ||
| 170 | 170 | * [LCL UTF16String documentation](<http://lazarus-ccr.sourceforge.net/docs/lcl/lclproc/utf16string.html> "doc:lcl/lclproc/utf16string.html") | |
| 171 | 171 | ||
| 172 | 172 | ## WideString | |
| 173 | 173 | ||
| 174 | 174 | 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. | |
| 175 | 175 | ||
| 176 | 176 | 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. | |
| 177 | 177 | ||
| 178 | - | ||
| 179 | - | ||
| 180 | - | ||
| 181 | - | ||
| 178 | + | | | | | | | a | | b | | c | #0 | #0 | | |
| 179 | + | |---|---|---|---|---|---|---|---|---|---|---|---| | |
| 180 | + | | Length | | | | | | | | | | | | | |
| 181 | + | ||
| 182 | 182 | #### Reference | |
| 183 | 183 | ||
| 184 | 184 | * [FPC WideString documentation](<http://www.freepascal.org/docs-html/ref/refsu11.html#x34-440003.2.5>) | |
| 185 | 185 | ||
| 186 | 186 | ## PShortString | |
| 187 | 187 | ||
| 188 | 188 | 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). | |
| 189 | 189 | ||
| 190 | - | ||
| 191 | - | ||
| 192 | - | ||
| 193 | - | ||
| 190 | + | | #3 | a | b | c | | |
| 191 | + | |---|---|---|---| | |
| 192 | + | | ^ | | | | | |
| 193 | + | ||
| 194 | 194 | #### Reference | |
| 195 | 195 | ||
| 196 | 196 | * [RTL PShortString documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pshortstring.html> "doc:rtl/system/pshortstring.html") | |
| 197 | 197 | ||
| 198 | 198 | ## PAnsiString | |
| 199 | 199 | ||
| 200 | 200 | 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**. | |
| 201 | 201 | ||
| 202 | - | ||
| 203 | - | ||
| 204 | - | ||
| 205 | - | ||
| 202 | + | | | | | | | | | | a | b | c | #0 | | |
| 203 | + | |---|---|---|---|---|---|---|---|---|---|---|---| | |
| 204 | + | | RefCount | | | | Length | | | | ^ | | | | | |
| 205 | + | ||
| 206 | 206 | #### Reference | |
| 207 | 207 | ||
| 208 | 208 | * [RTL PAnsiString documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pansistring.html> "doc:rtl/system/pansistring.html") | |
| 209 | 209 | ||
| 210 | 210 | ## PUnicodeString | |
| 211 | 211 | ||
| 212 | 212 | Variables of type **PUnicodeString** are pointers to variables of type **UnicodeString**. | |
| 213 | 213 | ||
| 214 | - | ||
| 215 | - | ||
| 216 | - | ||
| 217 | - | ||
| 214 | + | | | | | | | | | | | a | | b | | c | #0 | #0 | | |
| 215 | + | |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---| | |
| 216 | + | | RefCount | | | | Length | | | | ^ | | | | | | | | | |
| 217 | + | ||
| 218 | 218 | #### Reference | |
| 219 | 219 | ||
| 220 | 220 | * [RTL PUnicodeString documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/punicodestring.html> "doc:rtl/system/punicodestring.html") | |
| 221 | 221 | ||
| 222 | 222 | ## PWideString | |
| 223 | 223 | ||
| 224 | 224 | Variables of type **PWideString** are pointers. They point to the first char of a **WideString** -typed variable. | |
| 225 | 225 | ||
| 226 | - | ||
| 227 | - | ||
| 228 | - | ||
| 229 | - | ||
| 226 | + | | | | | | | a | | b | | c | #0 | #0 | | |
| 227 | + | |---|---|---|---|---|---|---|---|---|---|---|---| | |
| 228 | + | | Length | | | | ^ | | | | | | | | | |
| 229 | + | ||
| 230 | 230 | #### Reference | |
| 231 | 231 | ||
| 232 | 232 | * [RTL PWideString documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pwidestring.html> "doc:rtl/system/pwidestring.html") | |
| 233 | 233 | ||
| 234 | 234 | ## String constants | |
| 235 | 235 | ||
| 236 | 236 | 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. | |
| 237 | 237 | ||
| 238 | 238 | 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. | |
| 239 | 239 | ||
| 240 | - | ||
| 241 | - | ||
| 242 | - | ||
| 243 | - | ||
| 244 | - | ||
| 245 | - | ||
| 246 | - | ||
| 247 | - | ||
| 248 | - | ||
| 240 | + | | 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 | | |
| 241 | + | |---|---|---|---|---| | |
| 242 | + | | 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. | | |
| 243 | + | | 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 | | |
| 244 | + | | AnUnicodeString:='ãü'; | Without | Wrong everywhere | Wrong everywhere | Wrong everywhere | | |
| 245 | + | | 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 | | |
| 246 | + | | AnUTF8String:='ãü'; | Without | Same as AnsiString | Wrong everywhere | Wrong everywhere | | |
| 247 | + | | 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. | | |
| 248 | + | ||
| 249 | 249 | * W-WinAPI = Windows API "W" functions, UTF-16 | |
| 250 | 250 | * A-WinAPI = Windows API non "W" functions, 8bit system code page | |
| 251 | 251 | * System CP = The 8bit system code page of the OS. For example code page [1252](<http://en.wikipedia.org/wiki/Windows-1252>). | |
| 252 | 252 | ||
| 253 | 253 | ``` | |
| 254 | 254 | const | |
| 255 | 255 | c='ãü'; | |
| 256 | 256 | cstring: string = 'ãü'; // see AnAnsiString:='ãü'; | |
| 257 | 257 | var | |
| 258 | 258 | s: string; | |
| 259 | 259 | u: UnicodeString; | |
| 260 | 260 | begin | |
| 261 | 261 | s:=c; // same as s:='ãü'; | |
| 262 | 262 | s:=cstring; // does not change encoding | |
| 263 | 263 | u:=c; // same as u:='ãü'; | |
| 264 | 264 | u:=cstring; // fpc 2.6.1: converts from system cp to UTF-16, fpc 2.7.1+: depends on encoding of cstring | |
| 265 | 265 | end; | |
| 266 | 266 | ``` | |
| 267 | 267 | ||
| 268 | 268 | ## See also | |
| 269 | 269 | ||
| 270 | 270 | * [[FPC Unicode support]] | |
| 271 | 271 | * [[LCL Unicode Support]] | |
| 272 | 272 | * [[TStringList-TStrings Tutorial]] | |
| 273 | 273 | * [A Brief History of Strings](<http://web.archive.org/web/20151125114234/http://www.codexterity.com/delphistrings.htm>) | |
| 274 | - | ||
| 275 | 274 | --- | |
| 276 | 275 | *From the Free Pascal wiki, page "Character and string types/ja", as the Internet Archive kept it on 2021-05-13, 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).* |
