A detailed analysis of the strings in Delphi

Source: Internet
Author: User
Tags ord

Just shallow analysis, so that everyone can quickly understand the string.

all of these codes are in the Delphi7 under test pass.

The string types in Delphi 4,5,6,7 include the following:

    • Short String ( Short String )
    • Long String ( Long String )
    • Wide String ( Wide String )
    • 0 Ending string ( null-terminated String ), PChar and character Arrays

1 , short string ( Short String )

Fixed length, the maximum number of characters is 255, and the short string becomes the length byte (length-byte) string, because the No. 0 element of a short string contains the length of the string (the number of characters in the string). So the default maximum length for shortstring is 256 bytes (255 characters + 1 length bytes =256), and there are two ways to declare a short string, as follows:

    1. Var
    2. s:shortstring; {255 character length, 256 bytes}
    3. S1:STRING[255]; {S1 and S have the same character type}
    4. Len:integer;
    5. Begin
    6. S: = ' Hello ';
    7. Len: = Ord (S[0]); {Len now contains the length of S as 5,ord function can convert a character type to an integer type}
    8. Len: = SizeOf (S); {Len now contains the size of the shortstring type, which is 256 bytes}
    9. End

In the example above, the string length of s can be obtained by s[0], and the length of a short string can also be determined by using length function.

You can use the subscript of an array to access a character in a particular position in the shortstring, using the following examples and notes:

    1. Var
    2. S:STRING[8];
    3. I:integer;
    4. Begin
    5. S: = ' a_pretty_darn_long_string ';
    6. {Because S is only 8 characters in size,
    7. So s actually stores the content as "A_pretty"}
    8. I: = 10;
    9. S[i]: = ' s ';
    10. {Because S is only 8 characters in size,
    11. Attempting to overwrite the 10th element will cause memory confusion}
    12. End

2 , long string ( Long String )

A long string (ansistring) is a dynamically allocated string whose size is limited only by the available memory. To declare a long string, simply use the keyword string without the size parameter.

The characters contained in Ansistring in Delphi 7 are stored in a single byte.

    1. Var
    2. s:string;

Because it is dynamically allocated, you can modify the string at once, without worrying about other effects or cross-border issues. The string type has no 0 elements, and attempting to access a string of 0 elements produces a compilation error.

Long string lengths can also be obtained through the length function, or long strings can be set by the SetLength procedure. It is allocated in memory as follows:

3 , wide string ( Wide String )

The size of a wide string, like a long string, is limited only by valid memory and is dynamically allocated.

In Delphi 7 Widestring is implemented as 2 bytes to store a character, it is very convenient to use widestring to handle multibyte characters. Such as:

    1. Var
    2. s:string;
    3. {The default string in Delphi 7 is equivalent to ansistring}
    4. ws:widestring;
    5. Begin
    6. S: = ' Hello World ';
    7. WS: = S;
    8. ShowMessage (S[1]); {There is no display at this time, because S[1] takes out half of the ' world '
    9. ShowMessage (Ws[1]); {Show ' World '}
    10. End

4 , 0 ending string ( null-terminated String ), PChar and character Arrays

In C and C + +, there is no real string data type, all by a null-terminated (0) character array, the character array has no length byte, so only the trailing null flag is used as the character ending flag of the string. And because Windows is written in C, many Windows functions use character arrays as parameters, but the Pascal string type is not a character array, because in order for Pascal strings to be compatible with Windows, a string array is required. The PChar type is exactly what this requires, and PChar is available wherever a character array is required.

Although both ansistring and widestring have been implemented with NULL

The corresponding Pansichar and Pwidechar, respectively, correspond to Ansichar characters and widechar characters.

For example: The Windows MessageBox function, which declares the following:

    1. function MessageBox (hwnd:hwnd; lptext, Lpcaption:pchar; utype:uint): Integer; stdcall;

The second and third parameters require a pointer to a character array, in order to call this function, there are three ways to implement the

1 , PChar () Type Conversions

    1. Var
    2. text:string;
    3. caption:string;
    4. Begin
    5. Text: = ' This is a test. ';
    6. Caption: = ' Test Message ';
    7. MessageBox (0, PChar (Text), PChar (Caption), 0);
    8. {This pchar is used to convert the string type to a null-terminated character}
    9. End

which

2 , PChar variables

Let's start with an implementation and see what the Pchar type is.

Run the following program

    1. Var
    2. Text:pchar; {declared as Pchar type}
    3. str:string; {declared as a string type}
    4. Begin
    5. Text: = ' This is a test. '; {All are given the same string}
    6. STR: = ' This is a test. ';
    7. ShowMessage (IntToStr (SizeOf (Text))); {4 bytes, essentially a pointer}
    8. ShowMessage (IntToStr (SizeOf (STR))); {also 4 bytes, also pointer}
    9. End

Through the program above, we know that text is just a pointer.

    1. Var
    2. Text:pchar;
    3. Begin
    4. Text: = ' This is a test. ';
    5. MessageBox (0, Text, ' Test Message ', 0);
    6. {Here the text is directly declared in order to Pchar type, string constants can be used directly}
    7. End

The pointer text points to a region of memory that contains a null end of the ' This is a test ' string. It is equivalent to the following code:

    1. Const
    2. TEMPSTRING:ARRAY[0..15] of Char = ' This is a test. ' #0;
    3. Var
    4. Text:pchar;
    5. Begin
    6. Text: = @TempString [0];
    7. {text points to the address of the No. 0 element of a null-terminated tempstring character array,
    8. The first address of the entire character array}
    9. MessageBox (0, Text, ' Test Message ', 0);
    10. End

3 , Char type character array

You can also use a char array instead of Pchar, the code is as follows:

  1. Var
  2. TEXT1:ARRAY[0..14] of Char; {size is 15 characters}
  3. TEXT2:ARRAY[0..20] of Char; {size is 21 characters}
  4. Begin
  5. Text1: = ' This is a test. '; {TEXT1 and Text2 have a character length of 15 characters}
  6. TEXT2: = ' This is a test. ';
  7. MessageBox (0, Text1, ' Test Message 1 ', 0);
  8. {because the Text1 character length exceeds its declared size, because memory access is confusing, displaying a scramble}
  9. MessageBox (0, Text2, ' Test Message 2 ', 0);
  10. {Text2 character length is smaller than the declared size because normal access is displayed correctly}
  11. End

The results appear as follows:

The string on the first shallow talk about this, and then in-depth understanding.

********************************************************************

Conversion between Delphi string, Pchar, and character array

var

s:string;

P:pchar;

A:ARRAY[1..20] of Char;

1. String---> PChar

P: = PChar (s);

2. PChar---> String

s: = P;

3. PChar---> Character array

Strcopy (@a, p);

4, character array---> PChar

PChar (@a);

5. String---> Character array

Strcopy (@a, PChar (s));

6. Character array---> string

S: = PChar (@a);

"Note" The conversion between the string and the character array with PChar;

A detailed analysis of the strings in Delphi

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.