STRING

Top  Previous  Next

STRING is one of the basic data types. A variable of the type STRING is used for processing strings of text for example when sending messages over SMS or on a display.

 

A STRING data type occupies 2 bytes of storage because the actual string is handled separately and is subject to automatic garbage collection.
 

The maximum length of a string is 16384 characters when Large String Support (LSS) has been enabled in the project settings. Without LSS the maximum size is just 254 characters.

Internally, strings are zero-terminated and therefore the string itself cannot contain the character 0 (zero).

 

 

In order to include special characters in strings, it is possible to use the following control character strings:

 

$$  --> $

$"  --> "

$L  --> linefeed (value 10)

$P  --> formfeed (value 12)

$R  --> carriage return (value 13)

$T  --> Tab (value 9)

$N  --> Newline (value 13, value 10)

$xx --> Value xx (xx in hex, 00..FF)

 

 

Please note that when assigning a variable of the type STRING to another variable of the same type, the contents are not copied. The assignment only has the effect of making the two variables point to the same string.

 

 

Dynamic strings:

 

A dynamic string is a string that must be created and stored in RAM at run time.

 

If you have declared:

 

A : STRING;

 

and you assign:

 

A:="Hello world";

 

this will not be a dynamic string but a static string and will not occupy any space in RAM.

 

If you assign:

 

A:=strConcat(str1:="Hello ",str2:="world");

 

it will be a dynamic string that will occupy the string-length + 1. In this case 12 bytes.

 

Please see the section on Operators for more information.

 

 

The following resource limitations apply:

X32 Execution Architecture:

Maximum 400 dynamic strings with a total size of 62 Kbytes.

NX32 Execution Architecture:

Maximum 600 dynamic strings with a total size of 125 Kbytes.

NX32L Execution Architecture:

Maximum 4096 dynamic strings with a total size of 512 Kbytes.

 

Important note regarding the use of strings with multithreading:

Assigning a dynamic string to a global (shared) STRING variable from multiple threads is not thread-safe. An example is assigning a global string to a dynamic string created, for example, by strConcat() or the '+' operator. A MUTEX can be used to protect the operation in this special case.

Failure to follow these guidelines can result in a fault code 6 (Illegal string-id referenced). Please see the section on fault codes.

 

Example:

INCLUDE rtcu.inc
 
VAR
  text_to_send : STRING := "This is the message";
  str         : STRING;
  str2         : STRING;
END_VAR;
 
PROGRAM test;
 
BEGIN
  ...
  gsmSendSMS(number := "+44 12 34 56 789", message := text_to_send);
  str := "This is a test $NThis is placed on a newline";
  str2 := "Hello" + " " + "World!"; // Result: "Hello World!".
  ...
END;
 
END_PROGRAM;