Use strings in Delphi

Source: Internet
Author: User
Tags types of functions
I. Various strings
String is the most useful type in all data types of Object Pascal. Many functions use strings as transmission parameters. In Delphi, strings are defined and used in various ways, including typical strings in Pascal and long strings supported by Delphi (ansistring ), similar to the C character array (array of char), pointer to character (pchar), etc. The following article will talk about the differences and precautions of these types in definition and application.
1. Traditional Pascal string
In PASCAL, a typical string is a string of a certain length. Each string has a set length (the default value is 255). The following is an example:
VaR
Address: string;
Code: String [50];
Address is a string of 255 characters and the maximum code length is 50.
The length of a traditional Pascal string cannot exceed 255.
You can use the string connection operation "+" to connect strings together:
Result: = string1 + string2;
2. long strings in Delphi
In addition to traditional Pascal short strings, Delphi also supports long strings. Long characters are called ansistring. Long String dynamically allocates memory, that is, the memory required by the string is allocated only when the string is used, so its length is not limited. If you use string1: string as the type description in Delphi, string1 may be either a short string or a long string, depending on the $ H switch settings in the compiler. The default value is $ H +, which indicates an ANSI long string. Components in VCL use an ANSI long string. The long string ends with null, which means that the long string is fully compatible with the string ending with null in C.
You can use the setlength function to set the maximum length of a string:
Setlength (string1, 100); Use the trimleft, trimright, and trim functions to remove the blank spaces at the beginning, end, and end of a string.
3. A character array similar to C
The string ending with null can be stored in an array starting with 0. Definition:
VaR
Name: array [0 .. 50] of char;
4. pchar pointer
If exended syntax in Delphi has been set (default), the string array starting from 0 is fully compatible with the pointer pchar pointing to the character, because the character array name starting from 0 points to the first character pointer of the character array. You can directly pay the value of the string to the pchar pointer. For example:
VaR
P: pchar;
Begin
P: = 'Hello world ';
End;
In this way, P points to a piece of memory that stores the string 'Hello world' and ends with null.
Many windows application interface API functions require the pchar type as parameters. When pchar pointer is used, the getmem (var p: pointer; Size: integer) function is used to apply for memory allocation. When the program ends, freemem (var p: pointer [; Size: integer]) is used. function releases memory. For example:
VaR WINDIR, Sysdir: pchar;
Begin
Getmem (WINDIR, 256); {allocate memory for pointer}
Getwindowsdirectory (WINDIR, 128); {place the Windows Installation Directory to WINDIR}
Showmessage ('windows directory is '+ WINDIR); {display result}
End;
Ii. String Conversion
The above describes the definition and use of the four types of strings in Delphi. Because different types of functions have different types of string parameters, string type conversion is required.
1. You can use strpas to convert a null string to a Pascal short string. Strpcopy completes the opposite conversion.
2. Because the long string ends with null, you can use forced type conversion to convert the long string to pchar type. Usage: pchar (s), S is a long string. Forced type conversion returns a pointer to the first character of a long string and ends with null. For example:
VaR
Caption, message: string;
Caption: = 'Hello world! ';
Mssage: = 'this is a test of long string ';
MessageBox (0, pchar (Message), pchar (Caption), mb_ OK );
Conclusion: when using a string in Delphi, you must always be clear about the string type to avoid confusion. When you understand a string, you need to associate the string with the pointer and memory allocation to enhance understanding.

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.