ShortString is maintained for backward compatibility only. If S is a ShortString variable, Ord(S), like Length(S), returns the length of S assigning a value to S, like calling SetLength, changes the length of S. While the length of a ShortString can change dynamically, its memory is a statically allocated 256 bytes the first byte stores the length of the string, and the remaining 255 bytes are available for characters. Examples:Ī ShortString is 0 to 255 single-byte characters long. The length of the string changes dynamically when the assignment is made. You can assign the value of a string constant - or any other expression that returns a string - to a variable. Also, avoid passing string indexes as var parameters, because this results in inefficient code. The following code uses the standard UpCase function to convert MyString to uppercase:īe careful indexing strings in this way, since overwriting the end of a string can cause access violations. For single-byte (Western) locales, MyString := 'A' assigns the value A to the second character of MyString. SizeOf returns the number of bytes in a pointer for all other string types, since they are pointers.įor a short string or AnsiString, S is of type AnsiChar. Note that SizeOf returns the number of characters in a string only for a short string. Note that the SizeOf function returns the number of bytes used to represent a variable or type. The SetLength procedure adjusts the length of a string. As noted above, the number of elements is not necessarily the number of characters. The standard function Length returns the number of elements in a string. However, if some characters are not in the BMP, an indexed element may be a surrogate pair - not an entire character. If the string contains characters in the Basic Multilingual Plane (BMP), all characters are 2 bytes, so indexing the string gets characters. Similarly, indexing a UnicodeString variable results in an element that may not be an entire character. If S is a non- UnicodeString string variable and i, an integer expression, S represents the ith byte in S, which may not be the ith character or an entire character at all for a multibyte character string (MBCS). You can index a string variable just as you would an array. Zero-length strings represent the lowest values. For example, 'AB' is greater than 'A' that is, 'AB' > 'A' returns True. Between strings of unequal length, each character in the longer string without a corresponding character in the shorter string takes on a greater-than value. Note that the keyword string is also used when declaring ShortString types of specific lengths (see Short Strings, below).Ĭomparison of strings is defined by the ordering of the elements in corresponding positions. This is a potentially useful technique when using older 16-bit Delphi code or Turbo Pascal code with your current programs. On the Win32 platform, you can use the directive to turn string into ShortString. On the Win32 platform, the compiler interprets string (when it appears without a bracketed number after it) as UnicodeString. For example:Ĭreates a variable S that holds a string. The reserved word string functions like a general string type identifier. It is a code paged AnsiString type with a UTF-8 code page.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |