Sign in Join
Wiki copytemporary

Character and string types/ja: what changed

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

OldNewLine
11Free Pascal は、各種の **[[Char/ja|文字]] と [[String/ja|文字列]]** を扱えます。それらは ANSIキャラクタ から ユニコード文字列、また 各種ポインタも含みます。違いは、encodings と reference counting にも影響します。
22
33## AnsiChar
44
55A variable of type **AnsiChar** , also referred to as **[[Char|char]]** , is exactly 1 byte in size, and contains one ANSI character.
66
7-a
8----
9-
7+| a |
8+|---|
9+
1010#### Reference
1111
1212 * [FPC AnsiChar documentation](<http://www.freepascal.org/docs-html/ref/refsu7.html>)
1313 * [[Char|Usage Char]]
1414
1515## WideChar
1616
1717A 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.
1818
19-| a
20----|---
21-
19+| | a |
20+|---|---|
21+
2222#### References
2323
2424 * [FPC WideChar documentation](<http://www.freepascal.org/docs-html/ref/refsu8.html>)
2525 * [UTF-16 information on Wikipedia](<https://en.wikipedia.org/wiki/UTF-16>)
2626 * [RTL UnicodeChar documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/unicodechar.html> "doc:rtl/system/unicodechar.html")
2727
2828## Array of Char
2929
3030Early 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.
3131
3232### Static Array of Char
3333```
3434type
3535 TOldString4 = array[0..3] of char;
3636 var
3737 aOldString4: TOldString4;
3838 begin
3939 aOldString4[0] := 'a';
4040 aOldString4[1] := 'b';
4141 aOldString4[2] := 'c';
4242 aOldString4[3] := 'd';
4343 end;
4444```
4545
4646The static array of char has now the content:
4747
48-a | b | c | d
49----|---|---|---
50-
48+| a | b | c | d |
49+|---|---|---|---|
50+
5151[![Note-icon.png](/wiki-images/b/be/Note-icon.png)](</File:Note-icon.png>)
5252
5353**Note:** Unassigned chars can have any content, depending on what was just in memory when the memory for the array was made available.
5454
5555### Dynamic Array of Char
5656```
5757var
5858 aOldString: Array of Char;
5959 begin
6060 SetLength(aOldString, 5);
6161 aOldString[0] := 'a';
6262 aOldString[1] := 'b';
6363 aOldString[2] := 'c';
6464 aOldString[3] := 'd';
6565 end;
6666```
6767
6868The dynamic array of char has now the content:
6969
70-a | b | c | d | #0
71----|---|---|---|---
72-
70+| a | b | c | d | #0 |
71+|---|---|---|---|---|
72+
7373[![Note-icon.png](/wiki-images/b/be/Note-icon.png)](</File:Note-icon.png>)
7474
7575**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 ...)
7676
7777## PChar
7878
7979A 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.
8080
81-a | b | c | #0
82----|---|---|---
83-^
84-
81+| a | b | c | #0 |
82+|---|---|---|---|
83+| ^ | | | |
84+
8585#### Reference
8686
8787 * [FPC PChar documentation](<http://www.freepascal.org/docs-html/ref/refsu13.html>)
8888 * [PChar related functions](<http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/pcharfunctions.html> "doc:rtl/sysutils/pcharfunctions.html")
8989
9090## PWideChar
9191
9292A variable of type **PWideChar** is a pointer to a WideChar variable.
9393
94-| a | | b | | c | #0 | #0
95----|---|---|---|---|---|---|---
96-^
97-
94+| | a | | b | | c | #0 | #0 |
95+|---|---|---|---|---|---|---|---|
96+| ^ | | | | | | | |
97+
9898#### Reference
9999
100100 * [RTL PWideChar documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pwidechar.html> "doc:rtl/system/pwidechar.html")
101101
102102## String
103103
104104The 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**.
105105
106106#### Reference
107107
108108 * [[String|Usage String]]
109109 * [String functions](<http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/stringfunctions.html> "doc:rtl/sysutils/stringfunctions.html")
110110 * [Reference for unit 'strutils': Procedures and functions](<http://lazarus-ccr.sourceforge.net/docs/rtl/strutils/index-5.html> "doc:rtl/strutils/index-5.html")
111111
112112## ShortString
113113
114114Short 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.
115115
116-#3 | a | b | c
117----|---|---|---
118-
116+| #3 | a | b | c |
117+|---|---|---|---|
118+
119119#### Reference
120120
121121 * [FPC AnsiString documentation](<http://www.freepascal.org/docs-html/ref/refsu10.html>)
122122
123123## AnsiString
124124
125125Ansistrings 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.
126126
127-| | | | | | | | a | b | c | #0
128----|---|---|---|---|---|---|---|---|---|---|---
129-RefCount | Length
130-
127+| | | | | | | | | a | b | c | #0 |
128+|---|---|---|---|---|---|---|---|---|---|---|---|
129+| RefCount | | | | Length | | | | | | | |
130+
131131#### Reference
132132
133133 * [FPC AnsiString documentation](<http://www.freepascal.org/docs-html/ref/refsu10.html>)
134134
135135## UnicodeString
136136
137137Like **AnsiStrings** , **UnicodeStrings** are reference counted, null-terminated arrays, but they are implemented as arrays of **WideChars** instead of regular **Chars**.
138138
139139[![Note-icon.png](/wiki-images/b/be/Note-icon.png)](</File:Note-icon.png>)
140140
141141**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)...
142142
143-| | | | | | | | | a | | b | | c | #0 | #0
144----|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---
145-RefCount | Length
146-
143+| | | | | | | | | | a | | b | | c | #0 | #0 |
144+|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
145+| RefCount | | | | Length | | | | | | | | | | | |
146+
147147#### Reference
148148
149149 * [FPC UnicodeString documentation](<http://www.freepascal.org/docs-html/ref/refsu11.html>)
150150
151151## UTF8String
152152
153153In 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
154154```
155155UTF8String = type AnsiString(CP_UTF8);
156156```
157157
158158It 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.
159159
160160#### Reference
161161
162162 * [FPC UTF8String documentation](<http://www.freepascal.org/docs-html/ref/refsu10.html#x33-410003.2.4>)
163163
164164## UTF16String
165165
166166The type **UTF16String** is an alias to the type **WideString**. In the LCL unit _lclproc_ it is an alias to **UnicodeString**.
167167
168168#### Reference
169169
170170 * [LCL UTF16String documentation](<http://lazarus-ccr.sourceforge.net/docs/lcl/lclproc/utf16string.html> "doc:lcl/lclproc/utf16string.html")
171171
172172## WideString
173173
174174Variables 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.
175175
176176WideStrings 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.
177177
178-| | | | | a | | b | | c | #0 | #0
179----|---|---|---|---|---|---|---|---|---|---|---
180-Length
181-
178+| | | | | | a | | b | | c | #0 | #0 |
179+|---|---|---|---|---|---|---|---|---|---|---|---|
180+| Length | | | | | | | | | | | |
181+
182182#### Reference
183183
184184 * [FPC WideString documentation](<http://www.freepascal.org/docs-html/ref/refsu11.html#x34-440003.2.5>)
185185
186186## PShortString
187187
188188A 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).
189189
190-#3 | a | b | c
191----|---|---|---
192-^
193-
190+| #3 | a | b | c |
191+|---|---|---|---|
192+| ^ | | | |
193+
194194#### Reference
195195
196196 * [RTL PShortString documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pshortstring.html> "doc:rtl/system/pshortstring.html")
197197
198198## PAnsiString
199199
200200Variables 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**.
201201
202-| | | | | | | | a | b | c | #0
203----|---|---|---|---|---|---|---|---|---|---|---
204-RefCount | Length | ^
205-
202+| | | | | | | | | a | b | c | #0 |
203+|---|---|---|---|---|---|---|---|---|---|---|---|
204+| RefCount | | | | Length | | | | ^ | | | |
205+
206206#### Reference
207207
208208 * [RTL PAnsiString documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pansistring.html> "doc:rtl/system/pansistring.html")
209209
210210## PUnicodeString
211211
212212Variables of type **PUnicodeString** are pointers to variables of type **UnicodeString**.
213213
214-| | | | | | | | | a | | b | | c | #0 | #0
215----|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---
216-RefCount | Length | ^
217-
214+| | | | | | | | | | a | | b | | c | #0 | #0 |
215+|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
216+| RefCount | | | | Length | | | | ^ | | | | | | | |
217+
218218#### Reference
219219
220220 * [RTL PUnicodeString documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/punicodestring.html> "doc:rtl/system/punicodestring.html")
221221
222222## PWideString
223223
224224Variables of type **PWideString** are pointers. They point to the first char of a **WideString** -typed variable.
225225
226-| | | | | a | | b | | c | #0 | #0
227----|---|---|---|---|---|---|---|---|---|---|---
228-Length | ^
229-
226+| | | | | | a | | b | | c | #0 | #0 |
227+|---|---|---|---|---|---|---|---|---|---|---|---|
228+| Length | | | | ^ | | | | | | | |
229+
230230#### Reference
231231
232232 * [RTL PWideString documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pwidestring.html> "doc:rtl/system/pwidestring.html")
233233
234234## String constants
235235
236236If 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.
237237
238238There 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.
239239
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-
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+
249249 * W-WinAPI = Windows API "W" functions, UTF-16
250250 * A-WinAPI = Windows API non "W" functions, 8bit system code page
251251 * System CP = The 8bit system code page of the OS. For example code page [1252](<http://en.wikipedia.org/wiki/Windows-1252>).
252252
253253```
254254const
255255 c='ãü';
256256 cstring: string = 'ãü'; // see AnAnsiString:='ãü';
257257 var
258258 s: string;
259259 u: UnicodeString;
260260 begin
261261 s:=c; // same as s:='ãü';
262262 s:=cstring; // does not change encoding
263263 u:=c; // same as u:='ãü';
264264 u:=cstring; // fpc 2.6.1: converts from system cp to UTF-16, fpc 2.7.1+: depends on encoding of cstring
265265 end;
266266```
267267
268268## See also
269269
270270 * [[FPC Unicode support]]
271271 * [[LCL Unicode Support]]
272272 * [[TStringList-TStrings Tutorial]]
273273 * [A Brief History of Strings](<http://web.archive.org/web/20151125114234/http://www.codexterity.com/delphistrings.htm>)
274-
275274---
276275*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).*

Back to history