The following types are covered:. A string represents a sequence of characters. Delphi supports the following predefined string types.
Unicode characters; multiuser servers and multilanguage applications. WideString is not supported by the Delphi compilers for mobile platforms, but is supported by the Delphi compilers for desktop platforms.
Using UnicodeString is preferred to WideString. String types can be mixed in assignments and expressions; the compiler automatically performs required conversions. But strings passed by reference to a function or procedure as the var and out parameters must be of the appropriate type. Strings can be explicitly cast to a different string type.
However, casting a multibyte string to a single byte string may result in data loss. The reserved word string functions like a general string type identifier. For example:. On the Win32 platform, the compiler interprets string when it appears without a bracketed number after it as UnicodeString. This is a potentially useful technique when using older bit Delphi code or Turbo Pascal code with your current programs. Note that the keyword string is also used when declaring ShortString types of specific lengths see Short Strings , below.
Comparison of strings is defined by the ordering of the elements in corresponding positions. 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.
Zero-length strings represent the lowest values. You can index a string variable just as you would an array. If S is a non- UnicodeString string variable and i , an integer expression, S[i] 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.
Similarly, indexing a UnicodeString variable results in an element that may not be 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.
However, if some characters are not in the BMP, an indexed element may be a surrogate pair - not an entire character. The standard function Length returns the number of elements in a string. As noted above, the number of elements is not necessarily the number of characters. The SetLength procedure adjusts the length of a string. Note that the SizeOf function returns the number of bytes used to represent a variable or type.
Note that SizeOf returns the number of characters in a string only for a short string. SizeOf returns the number of bytes in a pointer for all other string types, since they are pointers. The following code uses the standard UpCase function to convert MyString to uppercase:.
Be careful indexing strings in this way, since overwriting the end of a string can cause access violations. Also, avoid passing string indexes as var parameters, because this results in inefficient code. I recommend using long strings whenever you can. The s variable can hold from zero to any practical number of characters. The string grows or shrinks as you assign new data to it. We can use any string variable as an array of characters, the second character in s has the index 2.
The following code. Now the few of the first characters in s look like: TTe s str Don't be mislead, you can't use s[0] to see the length of the string, s is not ShortString.
Since memory allocation is done by Delphi, we don't have to worry about garbage collection. When working with Long Ansi Strings Delphi uses reference counting. This way string copying is actually faster for long strings than for short strings. Reference counting, by example:. When we create string s1 variable, and assign some value to it, Delphi allocates enough memory for the string.
When we copy s1 to s2 , Delphi does not copy the string value in memory, it only increases the reference count and alters the s2 to point to the same memory location as s1. To minimize copying when we pass strings to routines, Delphi uses copy-on-write technique. Suppose we are to change the value of the s2 string variable; Delphi copies the first string to a new memory location, since the change should affect only s2, not s1, and they are both pointing to the same memory location.
Wide strings are also dynamically allocated and managed, but they don't use reference counting or the copy-on-write semantics. Wide strings consist of bit Unicode characters.
Unicode stores each character in the character set in 2 bytes instead of 1. Some national languages use ideographic characters, which require more than the characters supported by ANSI. With bit notation we can represent 65, different characters. This is a classic trade-off between memory and speed: by storing data in different memory locations and not using portions of a single location you gain extra runtime speed, although this is costing extra memory for each and every string you create.
While in the past you had to use low-level pointer-based code to access to the reference count, the Delphi RTL adds some handy functions to access the various string metadata:.
There is also a new helper functions in the SysUtils unit, called ByteLength, that returns the size of a UnicodeString in bytes ignoring the StringElementSize attributes so, oddly enough, it won't work with string types other than UnicodeString. There are helper functions to access the information you'll generally need to use. In the code above and its output you can clearly notice that there isn't a direct call to determine the length of a string in bytes, since Length returns the number of characters.
Re:Long String. Patrick Both. Horst Kraem. Frank Peel. Re:Long String Quote Assassin wrote in message Robert Javorni. Q: Fast replacing short strings in a long string 2. Longer strings 4. Long Strings 6.
0コメント