Difference between revisions of "ShortString"

From Lazarus wiki
Jump to navigationJump to search
(add warning regarding low)
Line 29: Line 29:
 
     s := 4;
 
     s := 4;
 
</syntaxhighlight>
 
</syntaxhighlight>
+
 
 +
== Caveats ==
 +
{{Warning|<syntaxhighlight lang="pascal" inline>low(myShortString)</syntaxhighlight> returns <syntaxhighlight lang="pascal" inline>0</syntaxhighlight>, i. e. the index of the length Byte, not the first character’s index. Likewise, <syntaxhighlight lang="pascal" inline>high(myShortString)</syntaxhighlight> always returns <syntaxhighlight lang="pascal" inline>255</syntaxhighlight>. Use <syntaxhighlight lang="pascal" inline>length</syntaxhighlight> instead.}}
 +
Nevertheless, a [[for-in loop|<syntaxhighlight lang="pascal" inline>for … in</syntaxhighlight> loop]] will work as expected.
 +
 
 
== See also ==
 
== See also ==
 
 
* [[Data type|Data types]]
 
* [[Data type|Data types]]
 
* [[Character and string types]]
 
* [[Character and string types]]

Revision as of 16:03, 14 February 2021

English (en)

Memory requirement: 256 bytes (1 byte for the length specification and 255 bytes for the characters).

Property: The data field of the data type Shortstring is an array, made up of data fields of the data type Char. Its length is defined as: ShortString = String[255];

ShortString has the same properties as the string in Turbo Pascal.

Definition of a data field of the data type ShortString:

  var 
    s : ShortString;

Examples for the valid assignment values:

    s := '0123ABCabc456';
    s := s + '! "§ $% & / () =?';
    s := 'c';
    s := s + IntToStr (45);

Examples of invalid assignment of values:

    s := True;
    s := 4;

Caveats

Warning-icon.png

Warning: low(myShortString) returns 0, i. e. the index of the length Byte, not the first character’s index. Likewise, high(myShortString) always returns 255. Use length instead.

Nevertheless, a for  in loop will work as expected.

See also