Documentation

String Type

The string type is used for storing text strings. A text string is a sequence of characters in the Unicode format with the final zero at the end of it. A string constant can be assigned to a string variable. A string constant is a sequence of Unicode characters enclosed in double quotes: "This is a string constant".

The internal representation is the 8-byte large structure. The first element of the structure is an integer of the int buf_len type, it contains the size of the buffer allocated for the string. The second element of the buf_ptr structure is the 32-bit pointer to a buffer containing the string. Right before the buffer the str_len variable of the int type is placed; it contains the string length (number of characters in the string to the final zero).

The member of the buf_len structure can take one of the following values:

  • 0 – indicates that this is a constant string that is stored in a pool of constant strings;
  • -1 – indicates that the line belongs to the client terminal (this line is cleared by the client terminal, and not by the performing subsystem);
  • number - the size of the buffer in characters of the short type placed in the computer memory to store the string.

string

Tight before the buffer, there are 4 bytes for the service information about the string length. Information about the string length is obtained by the StringLen() function from this place. The string length can be of zero size, even if the buffer allocated for the string is of the non-zero size. The string end in the buffer is always at the place where where first byte containing 0 is met.

If you need to include a double quote (") into a string, the backslash character (\) must be put before it. Any special character constants can be written in a string, if the backslash character (\) is typed before them.

Examples:

string svar="This is a character string";
string svar2=StringSubstr(svar,0,4);
Print("Copyright symbol\t\x00A9");
FileWrite(handle,"This string contains a new line symbols \n");
string MT5path="C:\\Program Files\\MetaTrader 5";

If a variable of the string type is not explicitly initialized, the compiler adds the code that resets the specified structure. It is necessary for avoiding critical errors when calling such a variable.

See also

Conversion Functions, String Functions, FileOpen, FileReadString, FileWriteString