Skip to main content

Data Type: STRING

Important

In the case of Latin-1 encoding, the memory required for a STRING variable is always 1 byte per character plus one additional byte. For example, this results in 81 bytes in the case of a standard STRING(80) declaration.

In the case of UTF-8 encoding, a character can be up to 4 bytes long. However, the length of the string always indicates the number of bytes which are reserved for the STRING. Therefore, there is no longer a 1:1 relationship between the character count and the string length.

The STRING data type is interpreted as Latin-1 or UTF-8: If the UTF-8 Encoding for STRING compile option is enabled, then all string literals are interpreted in UTF-8 format.

Declaration:

<variable name> : STRING( <size> ) := ' <text> ';

<variable name> : STRING[ <size> ] := ' <text> ';

The memory size <size> and initialization are optional.

Example 207. Example
sVar : STRING(46):= 'This is a string with memory for 46 characters.';
sVar_µ : STRING[10] := 'µ (Mü)';
sVar_ß : STRING[10] := 'Eszett';


A variable of data type STRING can contain any string. The amount of <size> memory which is reserved during a declaration refers to the number of bytes and is shown in parentheses or brackets. If a size is not defined, then 80 characters are reserved by default. If a variable is initialized with a literal which is too long for the data type, then the literal is truncated accordingly from the right.

The length of the strings is however not fundamentally limited. When you execute string manipulations with the functions of the Standard library, strings up to a length of 255 are processed correctly. When you execute string manipulations with the functions of the StringUtils library, you can also use longer strings (>255 characters).

When a variable of the data type STRING is reinitialized by resetting the application, the contents of the (old) string, which exist after the terminating null character of the initial value, are not overwritten. This applies both to initialization with the initialization value and to initialization with the default initialization value 0.

In compiler version 3.5.18.0 and higher, you can select the encoding format of strings. You have the possibility to specify project-wide whether strings are interpreted in Latin-1 format or UTF-8 format. For UTF-8 encoding, enable the UTF8 Encoding for STRING compile option.

For more information, see: Compile options and UTF-8 Encoding

Tip

Before you change the encoding format, it is advisable to check your code with static analysis rule SA0175.

For more information, see: Static Analysis Light

Tip

Add the {attribute 'monitoring_encoding' := 'UTF8'} pragma to the declaration of a variable. Then you can monitor the contents of the variable in UTF8 encoding.

For more information, see: Pragma: monitoring_encoding