// The most common stringvar STR: string; {definition} begin STR: = ''; {value assignment} showmessage (inttostr (length (STR); {length is: 4} end;
// Long string ansistring; by default in the current version (2007), string is ansistringvar STR: ansistring; begin STR: = 'case '; showmessage (inttostr (length (STR); {length: 4} end;
// Wide string widestring (less efficient than ansistring) var STR: widestring; begin STR: = 'case'; showmessage (inttostr (length (STR); {the length is: 2} end;
// Fixed-length string var str1: String [6]; {the specified size cannot exceed 255} str2: String [100]; begin {memory occupied when the value is less} str1: = 'case'; showmessage (str1); {In case} showmessage (inttostr (length (str1); {4; this is the string length} showmessage (inttostr (sizeof (str1); {7; this is the memory size} {if it is given more, it will be truncated} str1: = 'in case of Delphi blog'; showmessage (str1); {In case} showmessage (inttostr (length (str1); {6; this is the actually saved string length} showmessage (inttostr (sizeof (str1); {7; this is the memory size} {question: isn't the declared size 6? Why is sizeof 7? } {Because a fixed-length string will have an extra first byte to remember the actual length of the string} {for example, if str2 is assigned the following value, then its first byte (str2 [0]) the character 'a'} str2: = 'iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii'; {65} showmessage (str2 [0]); {A} showmessage (inttostr (ord (str2 [0]); {65; this is the sequence number of 'A' in the ASCII sequence, is it used} {then can we use ord (str2 [0]) instead of length to determine the length of the string? } {You can set a fixed-length string. Not only can you read the string, but you can also set it like setlength} end;
// Response string; it is equivalent to string [255] var STR: Response string; begin STR: = 'in case of Delphi blog'; showmessage (STR ); {In case of Delphi blog} showmessage (inttostr (sizeof (STR); {256; this is the size} showmessage (inttostr (length (STR); {18; this is the actual length} showmessage (inttostr (ord (STR [0]); {18; this is the length retrieved from the first byte} end;