Character and string types/de

From Lazarus wiki
Revision as of 19:26, 4 June 2014 by Michl (talk | contribs) (translation)
Jump to navigationJump to search

Deutsch (de) English (en) español (es) français (fr) русский (ru) 中文(中国大陆)‎ (zh_CN)

FreePascal unterstützt verschiedene Arten von Zeichen und Zeichenfolgen. Sie reichen von einzelnen ANSI-Zeichen bis hin zu Unicode-Zeichenfolgen und beinhalten auch Zeigertypen. Unterschiede gelten auch für Codierungen und Referenzzählungen.

AnsiChar

Eine Variable vom Typ AnsiChar, auch Char genannt, ist genau 1 Byte groß und enthält ein ANSI-Zeichen.

a

Referenz

WideChar

Eine Variable vom Typ WideChar, auch als UnicodeChar bezeichnet, ist genau 2 Bytes groß und beinhaltet ein (oder ein Teil von einem) Unicode-Zeichen in UTF-16 codiert. Hinweis: Es ist nicht realisierbar alle Unicode-Codepunkte mit 2 Byte zu codieren. Es ist daher möglich, dass einzelnen Codepunkt mit 2 WideChars codiert sind.

a

Referenzen

Array of Char

Frühe Pascal-Implementierungen, die vor 1978 in Gebrauch waren, unterstützten keinen Stringtyp (mit Ausnahme von String-Konstanten). Die einzige Möglichkeit zur Speicherung einer Zeichenfolgen in einer Variablen war die Verwendung eines Array of Char. Dieser Ansatz hat viele Nachteile und wird nicht mehr empfohlen. Er wird jedoch immer noch unterstützt, um die Abwärtskompatibilität mit alten Code zu gewährleisten.

Statisches Array of Char

type
  TOldString4 = array[0..3] of char;
var
  aOldString4: TOldString4; 
begin
  aOldString4[0] := 'a';
  aOldString4[1] := 'b';
  aOldString4[2] := 'c';
  aOldString4[3] := 'd';
end;

The statische Array of Char hat nun den Inhalt:

a b c d
Light bulb  Hinweis: Nicht zugewiesene Zeichen können einen beliebigen Inhalt haben, je nachdem, was gerade im Speicher war, als der Speicher für das Array zur Verfügung gestellt wurde.

Dynamisches Array of Char

var
  aOldString: Array of Char; 
begin
  SetLength(aOldString, 5);
  aOldString[0] := 'a';
  aOldString[1] := 'b';
  aOldString[2] := 'c';
  aOldString[3] := 'd';
end;

Das dynamische Array of Char hat nun den Inhalt:

a b c d #0
Light bulb  Hinweis: Nicht zugewiesene Zeichen in einem dynamischen Array of Char haben den Inhalt #0, da freie Positionen aller dynamischen Arrays zunächst mit 0 initialisiert werden (oder #0 oder NULL, oder...)

PChar

Eine Variable vom Typ PChar ist im Grunde ein Zeiger auf einen [[Character_and_string_types/de#AnsiChar|Char] und ermöglicht so zusätzliche Operationen. PChars können verwendet werden, um auf C-Null-terminierte Zeichenfolgen, z. B. in der Interaktion mit bestimmten OS Bibliotheken oder Software von Drittanbietern zugreifen.

a b c #0
^

Reference

PWideChar

in Bearbeitung - --Michl 20:26, 4 June 2014 (CEST)

A variable of type PWideChar is a pointer to a WideChar variable.

a b c #0 #0
^

Reference

String

The type string may refer to ShortString or AnsiString, depending from the {$H} switch. 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.

See also

String
String functions
Reference for unit 'strutils': Procedures and functions

ShortString

Short strings have a maximum length of 255 characters with the implicit codepage CP_ACP. The length is stored in the character at index 0.

#3 a b c

Reference

AnsiString

Ansistrings or UTF8Strings are strings that have no length limit. They are reference counted and are guaranteed to be null terminated. 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.

a b c #0
RefCount Length

Reference

UnicodeString

Like AnsiStrings, UnicodeStrings are reference counted, null-terminated arrays, but they are implemented as arrays of WideChars instead of regular Chars.

Light bulb  Hinweis: 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)...
a b c #0 #0
RefCount Length

Reference

UTF8String

Currently, the type UTF8String is an alias to the type AnsiString. It is meant to contain UTF8 encoded strings (i.e. unicode data ranging from 1..4 bytes per character) UTF8String is the default string in Lazarus and LCL.

Reference

UTF16String

The type UTF16String is an alias to the type WideString. In the LCL unit lclproc it is an alias to UnicodeString.

Reference

WideString

Variables of type 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.

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.

a b c #0 #0
Length

Reference

PShortString

A variable of type PShortString is a pointer that points to the first byte of a ShortString-type variable.

#3 a b c
^

Reference

PAnsiString

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.

a b c #0
RefCount Length ^

Reference

PUnicodeString

Variables of type PUnicodeString are pointers to variables of type UnicodeString.

a b c #0 #0
RefCount Length ^

Reference

PWideString

Variables of type PWideString are pointers. They point to the first char of a WideString-typed variable.

a b c #0 #0
Length ^

Reference

See also

--Michl 13:32, 1 June 2014 (CEST)