Delphi characters and strings-Char, ansichar, widechar, pchar, pansichar, pwidechar

Source: Internet
Author: User
Delphi characters and strings-Char, ansichar, widechar, pchar, pansichar, pwidechar

Delphi has three types of characters:

Ansichar is a standard 1-byte ANSI character that programmers are familiar.

Widechar is a 2-byte UNICODE character.

Char is equivalent to ansichar at present, but it is equivalent to widechar in later versions of Delphi.

Remember that because a character does not represent one byte in length, it cannot be hard-coded in the application. Instead, you should use the sizeof () function. Note that the sizeof () standard function returns the type or the byte length of the instance.

 

A string is a variable type that represents a group of characters. each language has its own string type storage and usage methods.

Pascal has the following string types to meet program requirements:

Ansistring is the default string type of Pascal. It consists of ansichar characters. It has no length restrictions and is compatible with null-terminated strings.

This type is retained for backward compatibility with delphi1.0, and its length is limited to 255 characters.

Widestring is similar to ansistring, but it is composed of widechar characters.

Pchar refers to the pointer to the null-terminated char string, similar to the char * or lpstr type of C.

Pansichar points to the null-terminated ansichar string pointer.

Pwidechar points to the null-terminated widechar string pointer.

By default, if the following code is used to define a string, the compiler considers it an ansistring string:

1. ansistring type

The ansistring (or long string) type was introduced at delphi2.0, because delphi1.0 users need a string type that is easy to use and has no limit of 255 characters, ansistring can meet these requirements.

Although the ansistring type is almost the same as the previous string type in the External table, it is dynamically allocated and has the automatic recovery function, because this function ansistring is sometimes called the self-management type of survival. Objectpascal can allocate space for strings as needed, so it does not need to allocate a buffer for intermediate results as feared in C/C ++. In addition, the ansistring always ends with a null character, which makes the ansistring compatible with the string in WIN32API. In fact, the ansistring type is a pointer to the string structure in the stack.

The ansistring type has the reference count function, which means that several strings can point to the same physical address. Therefore, copying a string is faster because it only copies the pointer rather than the actual string. When two or more ansistring types share a reference pointing to the same physical address, Delphi memory management uses the copy-on-write technology, and the modification of a string ends, to release a reference and assign a physical string. The following example shows these concepts:

VaR

S1, S2: string;

Begin

// Assign a value to S1. The reference count of S1 is 1.

S1: = 'andnowforsomething ...';

S2: = S1; // now S2 and S1 point to the same string. The reference count of S1 is 2.

// S2 is changed now, so it is copied to its physical space, and the reference count of S1 is reduced by 1

S2: = S2 + 'completelydifferent1 ';

End;

Win32 compatibility

As mentioned above, the ansistring always ends with null. Therefore, it is compatible with strings ending with null, which makes it easy to call WIN32API functions or other functions that require pchar strings. You only need to forcibly convert a character type to the pchar type (in section 2.8 "forced type conversion and type conventions", we will introduce forced type conversion ). The following code demonstrates how to call the Win32 getwindowsdirectory () function. This function requires a pchar parameter:

VaR

S: string;

Begin

Setlength (S, 256); // important! First, allocate space to the string.

// Call the API function. s now contains the directory string

Getwindowsdirectory (pchar (s), 256 );

If you use a function and process to forcibly convert an ansistring to a pchar type, you must manually restore the length to the original length ending with null after use. The realizelenght () function in the strutils unit can achieve this:

Procedurerealizelength (vars: string );

Begin

Setlength (S, strlen (pchar (s )));

End;

Call reallizelength ():

VaR

S: string;

Begin

Setlength (S, 256); // important! First, allocate space to the string.

// Call the function. s now contains the directory string

Getwindowdirectory (pchar (s), 256 );

Realizelength (s); // set the length of S to the end length of null.

End;

Different from the ansistring type string, the semi string is incompatible with the string ending with null. This is because, when using the semi string to call the Win32 function, you need to do some work. The following shortstringaspchar () function is defined in the strutils. Pas unit.

Funcfunctionshortstringaspchar (vars: vars string): pchar;

{This function can end a string with null, so that it can be passed to the WIN32API function that requires pchar parameters. If the string contains more than 254 characters, the extra part will be truncated}

Begin

Iflength (S) = high (s) thendec (s [0]); {if S is too long, part of it will be intercepted}

S [ord (length (s) + 1]: = #0; {Add null to the end of the string}

Result: = @ s [1]; {returns a pchar string}

End;

The WIN32API function must end with a Null String. Do not pass the character string to the API function. Because the compiler reports an error, the long string can be passed to the WIN32API function.

Widestring type

The widestring type is the same as the ansistring type for self-management of lifetime. They can be dynamically allocated, automatically recycled, and compatible with each other. However, the differences between widestring and ansistring are mainly in three aspects:

Widestring consists of widechar characters, rather than ansichar characters, which are compatible with Unicode strings.

Widestring is allocated using the sysallocstrlen () API function. They are compatible with ole bstr strings.

Widestring does not reference the count. Therefore, when assigning a widestring to another widestring, You need to copy it from one location in the memory to another. This makes widestring less effective than ansistring in terms of speed and memory utilization.

As mentioned above, the compiler automatically converts variables of the ansistring type and widestring type. Example:

VaR

W: widestring;

S: string;

Begin

W: = 'privacy itaville ';

S: = W; // convert widestring to ansistring

S: = 'comemonday ';

W: = s; // ansistring to widestring

End;

To flexibly use the widestring type, objectpascal reloads Concat (), copy, insert (), length (), pos (), and setlength () and operators such as +, =, and <>.

Like the ansistring and semi string types, you can use the subscript of an array to access a specific character in widestring:

VaR

W: widestring;

C: widechar;

Begin

W: = 'ebonyandivorylivinginprefectharmony ';

C: = W [length (w)]; // C contains the last character of the W string

End;

String ending with null

As mentioned above, Delphi has three different string types ending with NULL: pchar, pansichar, and pwidechar. They are composed of three different characters of Delphi. The three types are consistent with pchar in general. Pchar is reserved to be compatible with delphi1.0 and WIN32API, and they need to use a string ending with null. pchar is defined as a string pointing to null (zero) the ending string pointer is different from the ansistring and widestring types. The pchar memory is not automatically generated and managed by objectpascal. The memory management function of Object Pascal is used to allocate the memory pointed to by pchar. The theoretical maximum length of a pchar string is 4 GB.

In most cases, the ansistring type can be used as pchar. You should try to use ansistring as much as possible because it automatically manages the string memory, this greatly reduces the amount of memory errors in applications. Therefore, we should try to avoid the use of pchar type and the corresponding manual memory allocation.

As mentioned above, the pchar variable requires manual allocation and release of memory for storing strings. Generally, the stralloc () function is used to allocate memory for the pchar buffer, but other functions can also be used to allocate the pchar type, including allocmem (), getmem (), strnew () and virtualalloc () API functions. These functions have corresponding functions to release memory.

--------------------------------------------------------------------------------

// Single-character char and ansichar (in the current version (2007), they are the same thing and only 1 byte in size)
VaR
C: Char; {the value range of the char type is #0 .. #255. It is represented in hexadecimal format: #$0 .. # $ ff}
Begin
{Value assignment in decimal format :}
C: = #65;
Showmessage (c); {}

{Assign values in hexadecimal format :}
C: = # $41;
Showmessage (c); {}

{Replace # With the CHR function}
C: = CHR (65 );
Showmessage (c); {}
C: = CHR ($41 );
Showmessage (c); {}

{Char length will certainly be 1}
Showmessage (inttostr (length (c); {1}

{Char and ansichar allow this convenient assignment (that is, it is compatible with a 1-byte string ):}
C: = 'B ';
Showmessage (c); {B}
End;
--------------------------------------------------------------------------------

// Unicode Character widechar; different from ansichar, widechar occupies 2 bytes.
VaR
C: widechar; {the value range of widechar is #0 .. #65535. The hexadecimal format is #$0 .. # $ FFFF}
Begin
{Widechar is compatible with #0 .. #255 of ansichar, but occupies 2 bytes}
C: = #65;
Showmessage (c); {}
Showmessage (inttostr (length (c); {1; this is the character length}
Showmessage (inttostr (sizeof (c); {2; but occupies 2 bytes}

{Assign values in hexadecimal notation}
C: = # $4e07;
Showmessage (c); {tens of thousands}
Showmessage (inttostr (length (c); {1; this is the character length}
Showmessage (inttostr (sizeof (c); {2; but occupies 2 bytes}

{Assign values in decimal format}
C: = #19975;
Showmessage (c); {tens of thousands}

{If the value does not exceed the range of #255, a value can be assigned directly}
C: = 'B ';
Showmessage (c); {tens of thousands}

{This is not acceptable}
// C: = 'wan'; {This is a problem supported by Delphi. It is estimated that Delphi 2008 can solve this problem}

{You can change it like this :}
C: = widestring ('wan') [1];
Showmessage (c); {tens of thousands}

{Display my name in widechar format}
Showmessage (#19975 #19968); {In case}
Showmessage (#19975 + #19968); {In case}
Showmessage (# $4e07 # $4e00); {In case}
End;
--------------------------------------------------------------------------------

// Character pointers: pchar and pansichar. In the current version (2007), there is no difference between them.
VaR
P: pchar;
STR: string;
Begin
{You can directly assign a String constant to pchar}
P: = 'case ';
Showmessage (p); {In case}
Showmessage (inttostr (length (p); {4}

{Convert variable values}
STR: = 'in case of Delphi blog ';
P: = pchar (STR); {conversion}
Showmessage (p); {in case of a Delphi blog}
Showmessage (inttostr (length (p); {18}
End;
--------------------------------------------------------------------------------

// Wide character pointer pwidechar
VaR
P: pwidechar;
STR: widestring; {Note that this is not a string}
Begin
{You can directly assign a String constant to pwidechar}
P: = 'case ';
Showmessage (p); {In case}
Showmessage (inttostr (length (p); {2}

{Convert variable values}
STR: = 'in case of Delphi blog ';
P: = pwidechar (STR); {conversion}
Showmessage (p); {in case of a Delphi blog}
Showmessage (inttostr (length (p); {13}
End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.