Strings in Delphi
——Excerpt from the Internet
1: Various strings
Strings are the most useful type of all data types in ObjectPascal. Many functions pass arguments with strings. Since there are various ways to define and use strings in Delphi, including typical strings (Strings) in Pascal, long strings supported by Delphi (ANSIStrings), character arrays similar to C language (ArrayofChar), and pointing to characters Pointer (Pchar), etc. The following article will talk about the differences and precautions of these types in definition and application.
1. Traditional Pascal strings
In Pascal, a typical string is a sequence of characters of a certain length. Each string has a set length (the default is 255), and the following is an example:
Var
Address:String;
Code:String[50];
Address is a string with a length of 255, and the maximum length of Code is 50.
The traditional Pascal string length cannot exceed 255.
You can use the string concatenation operation "+" to concatenate strings together:
Result:=String1+String2;
2. Long strings in Delphi
In addition to supporting traditional Pascal short strings, Delphi also supports long strings. Long characters are called ANSIString. Long strings dynamically allocate memory, that is, the memory required for strings is allocated only when using strings, so their length is not limited. In Delphi, if you use String1:String as type description, String1 may be either a short string or a long string, depending on the settings of the $H switch in the compiler. The default value is $H+, which represents ANSI long string. Components in VCL use ANSI long strings. Long strings end with null, which means that long strings are fully compatible with null-end strings in C language.
The maximum length of a string can be set through the SetLength function:
SetLength(String1,100); Use TrimLeft, TrimRight and Trim functions to eliminate the blank areas at the beginning, end and end of the string respectively.
3. Character array similar to C
An array starting with 0 can be used to store strings ending with null. The following definition:
Var
Name:Array[0..50] ofChar;
4. Pchar pointer
If the ExendedSyntax in Delphi has been set (default), the character array starting with 0 is fully compatible with the pointer Pchar pointing to the character, because the character array name starting with 0 is the pointer pointing to the first character of the character array. You can directly pay the string to the Pchar pointer. For example:
var
P:PChar;
Begin
P:='Helloworld';
end;
This way P points to a piece of memory that stores the string 'Helloworld' and ends with null.
Many Windows application program interface API functions require the Pchar type as parameter. When using the Pchar pointer, first uses the GetMem(varP:Pointer;Size:Integer) function to apply for allocation of memory. At the end of the program, use the FreeMem(varP:Pointer[;Size:Integer]) function to release memory. For example:
VarWinDir,SysDir:Pchar;
Begin
GetMem(WinDir,256);{allocate memory for pointers}
GetWindowsDirectory(WinDir,128);{Put the Windows installation directory to WinDir}
ShowMessage('Windowsdirectoryis'+WinDir);{Show result}
End;
Two: String conversion
The above introduces the definition and use of four types of strings in Delphi. Since various functions have different requirements for string parameter types, string type conversion is required.
1. You can use StrPas to convert a string ending with null to a Pascal short string. StrpCopy completes the opposite conversion.
2. Because long strings end with null, you can convert long strings to Pchar type with cast. Usage is: Pchar(s), s is a long string. Casting returns a pointer to the first character of the long string, and the pointed string ends with null. For example:
Var
Caption,Message:string;
Caption:='HelloWorld!';
Mssage:='Thisisatestoflongstring';
MessageBox(0,Pchar(Message),Pchar(Caption),MB_OK);
Summary: When using strings in Delphi, you must always be clear about the type of the string to avoid confusion. When understanding strings, you should link strings with pointers and memory allocations to enhance understanding.